Search in sources :

Example 11 with ByteArrayKeyValueDatabase

use of org.aion.db.impl.ByteArrayKeyValueDatabase in project aion by aionnetwork.

the class FvmContractDetailsTest method testDecodeFromEncodingWithMultiCodeAndLargeStorage.

@Test
public void testDecodeFromEncodingWithMultiCodeAndLargeStorage() {
    ByteArrayKeyValueDatabase db = new MockDB("db", log);
    db.open();
    assertThat(db.isEmpty()).isTrue();
    byte[] code1 = ByteUtil.hexStringToBytes("7c0100000000000000000000000000000000000000000000000000000000600035046333d546748114610065578063430fe5f01461007c5780634d432c1d1461008d578063501385b2146100b857806357eb3b30146100e9578063dbc7df61146100fb57005b6100766004356024356044356102f0565b60006000f35b61008760043561039e565b60006000f35b610098600435610178565b8073ffffffffffffffffffffffffffffffffffffffff1660005260206000f35b6100c96004356024356044356101a0565b8073ffffffffffffffffffffffffffffffffffffffff1660005260206000f35b6100f1610171565b8060005260206000f35b610106600435610133565b8360005282602052816040528073ffffffffffffffffffffffffffffffffffffffff1660605260806000f35b5b60006020819052908152604090208054600182015460028301546003909301549192909173ffffffffffffffffffffffffffffffffffffffff1684565b5b60015481565b5b60026020526000908152604090205473ffffffffffffffffffffffffffffffffffffffff1681565b73ffffffffffffffffffffffffffffffffffffffff831660009081526020819052604081206002015481908302341080156101fe575073ffffffffffffffffffffffffffffffffffffffff8516600090815260208190526040812054145b8015610232575073ffffffffffffffffffffffffffffffffffffffff85166000908152602081905260409020600101548390105b61023b57610243565b3391506102e8565b6101966103ca60003973ffffffffffffffffffffffffffffffffffffffff3381166101965285166101b68190526000908152602081905260408120600201546101d6526101f68490526102169080f073ffffffffffffffffffffffffffffffffffffffff8616600090815260208190526040902060030180547fffffffffffffffffffffffff0000000000000000000000000000000000000000168217905591508190505b509392505050565b73ffffffffffffffffffffffffffffffffffffffff33166000908152602081905260408120548190821461032357610364565b60018054808201909155600090815260026020526040902080547fffffffffffffffffffffffff000000000000000000000000000000000000000016331790555b50503373ffffffffffffffffffffffffffffffffffffffff1660009081526020819052604090209081556001810192909255600290910155565b3373ffffffffffffffffffffffffffffffffffffffff166000908152602081905260409020600201555600608061019660043960048051602451604451606451600080547fffffffffffffffffffffffff0000000000000000000000000000000000000000908116909517815560018054909516909317909355600355915561013390819061006390396000f3007c0100000000000000000000000000000000000000000000000000000000600035046347810fe381146100445780637e4a1aa81461005557806383d2421b1461006957005b61004f6004356100ab565b60006000f35b6100636004356024356100fc565b60006000f35b61007460043561007a565b60006000f35b6001543373ffffffffffffffffffffffffffffffffffffffff9081169116146100a2576100a8565b60078190555b50565b73ffffffffffffffffffffffffffffffffffffffff8116600090815260026020526040902080547fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0016600117905550565b6001543373ffffffffffffffffffffffffffffffffffffffff9081169116146101245761012f565b600582905560068190555b505056");
    byte[] code2 = ByteUtil.hexStringToBytes("60016002");
    AionAddress address = new AionAddress(RandomUtils.nextBytes(AionAddress.LENGTH));
    Map<ByteArrayWrapper, ByteArrayWrapper> storage = new HashMap<>();
    storage.put(ByteArrayWrapper.fromHex("18d63b70aa690ad37cb50908746c9a55"), ByteArrayWrapper.fromHex("00000000000000000000000000000064"));
    storage.put(ByteArrayWrapper.fromHex("18d63b70aa690ad37cb50908746c9a56"), ByteArrayWrapper.fromHex("0000000000000000000000000000000c"));
    storage.put(ByteArrayWrapper.fromHex("5a448d1967513482947d1d3f6104316f"), null);
    storage.put(ByteArrayWrapper.fromHex("5a448d1967513482947d1d3f61043171"), ByteArrayWrapper.fromHex("00000000000000000000000000000014"));
    storage.put(ByteArrayWrapper.fromHex("18d63b70aa690ad37cb50908746c9a54"), null);
    storage.put(ByteArrayWrapper.fromHex("5a448d1967513482947d1d3f61043170"), ByteArrayWrapper.fromHex("00000000000000000000000000000078"));
    storage.put(ByteArrayWrapper.fromHex("c83a08bbccc01a0644d599ccd2a7c2e0"), ByteArrayWrapper.fromHex("8fbec874791c4e3f9f48a59a44686efe"));
    storage.put(ByteArrayWrapper.fromHex("5aa541c6c03f602a426f04ae47508bb8"), ByteArrayWrapper.fromHex("7a657031000000000000000000000000"));
    storage.put(ByteArrayWrapper.fromHex("5aa541c6c03f602a426f04ae47508bb9"), ByteArrayWrapper.fromHex("000000000000000000000000000000c8"));
    storage.put(ByteArrayWrapper.fromHex("5aa541c6c03f602a426f04ae47508bba"), ByteArrayWrapper.fromHex("0000000000000000000000000000000a"));
    storage.put(ByteArrayWrapper.fromHex("00000000000000000000000000000001"), ByteArrayWrapper.fromHex("00000000000000000000000000000003"));
    storage.put(ByteArrayWrapper.fromHex("5aa541c6c03f602a426f04ae47508bbb"), ByteArrayWrapper.fromHex("194bcfc3670d8a1613e5b0c790036a35"));
    storage.put(ByteArrayWrapper.fromHex("aee92919b8c3389af86ef24535e8a28c"), ByteArrayWrapper.fromHex("cfe293a85bef5915e1a7acb37bf0c685"));
    storage.put(ByteArrayWrapper.fromHex("65c996598dc972688b7ace676c89077b"), ByteArrayWrapper.fromHex("d6ee27e285f2de7b68e8db25cf1b1063"));
    FvmContractDetails details = new FvmContractDetails(address, db);
    details.setCode(code1);
    details.setCode(code2);
    // update storage
    for (ByteArrayWrapper key : storage.keySet()) {
        ByteArrayWrapper value = storage.get(key);
        if (value == null) {
            details.delete(key);
        } else {
            details.put(key, value);
        }
    }
    // flush changes to the database
    details.syncStorage();
    FvmContractDetails decoded = FvmContractDetails.decodeAtRoot(fromEncoding(details.getEncoded()), db, details.getStorageHash());
    // check code
    byte[] codeHash = h256(code1);
    assertThat(decoded.getCode(codeHash)).isEqualTo(code1);
    codeHash = h256(code2);
    assertThat(decoded.getCode(codeHash)).isEqualTo(code2);
    // check address
    assertThat(decoded.address).isEqualTo(address);
    // check storage
    for (ByteArrayWrapper key : storage.keySet()) {
        ByteArrayWrapper value = storage.get(key);
        if (value == null) {
            assertThat(decoded.get(key)).isNull();
        } else {
            assertThat(decoded.get(key)).isEqualTo(value);
        }
    }
}
Also used : AionAddress(org.aion.types.AionAddress) ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) HashMap(java.util.HashMap) ByteArrayKeyValueDatabase(org.aion.db.impl.ByteArrayKeyValueDatabase) MockDB(org.aion.db.impl.mockdb.MockDB) Test(org.junit.Test)

Example 12 with ByteArrayKeyValueDatabase

use of org.aion.db.impl.ByteArrayKeyValueDatabase in project aion by aionnetwork.

the class AionRepositoryImplTest method testImportTrieNode.

@Test
public void testImportTrieNode() {
    AionRepositoryImpl repository = AionRepositoryImpl.createForTesting(repoConfig);
    ByteArrayKeyValueDatabase db = repository.getStateDatabase();
    byte[] nodeKey = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32 };
    ;
    byte[] altNodeKey = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 };
    byte[] smallNodeKey = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 };
    byte[] leafValue = new byte[] { -8, 114, -97, 60, -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, -72, 80, -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 };
    byte[] branchValue = new byte[] { -8, -111, -128, -96, -99, -57, 89, 41, -60, -8, -93, -128, 9, -59, -23, -116, 4, 70, -94, -76, 119, -16, 22, 117, -72, -96, -117, 125, -57, 95, 123, -29, -46, 37, -78, 86, -96, 89, -62, -94, 108, -21, -48, -19, 80, 5, 59, -70, 24, 90, 125, 19, -31, -82, 88, 49, 78, 44, 55, -44, 108, 31, 123, -120, 95, -39, 59, 104, 122, -96, -102, -109, -7, 6, 55, -12, 8, 32, -45, 116, 125, -50, 117, 7, 6, -59, -109, 88, 32, 95, 92, 97, 26, -107, -84, 85, 25, 9, 83, 78, -20, -37, -128, -128, -128, -128, -96, -35, -15, -76, -107, -93, -23, -114, 24, -105, -87, -79, 37, 125, 65, 114, -43, -97, -53, -32, -37, -94, 59, -117, -121, -127, 44, -94, -91, 89, 25, -39, -85, -128, -128, -128, -128, -128, -128, -128, -128 };
    byte[] emptyValue = new byte[0];
    // check import with empty database: TrieNodeResult.IMPORTED
    assertThat(db.isEmpty()).isTrue();
    assertThat(repository.importTrieNode(nodeKey, leafValue, DatabaseType.STATE)).isEqualTo(TrieNodeResult.IMPORTED);
    Optional<byte[]> value = db.get(nodeKey);
    assertThat(value.isPresent()).isTrue();
    assertThat(value.get()).isEqualTo(leafValue);
    // check import with same key + same value: TrieNodeResult.KNOWN
    assertThat(repository.importTrieNode(nodeKey, leafValue, DatabaseType.STATE)).isEqualTo(TrieNodeResult.KNOWN);
    value = db.get(nodeKey);
    assertThat(value.isPresent()).isTrue();
    assertThat(value.get()).isEqualTo(leafValue);
    // check import with same key + different value: TrieNodeResult.INCONSISTENT
    assertThat(repository.importTrieNode(nodeKey, branchValue, DatabaseType.STATE)).isEqualTo(TrieNodeResult.INCONSISTENT);
    value = db.get(nodeKey);
    assertThat(value.isPresent()).isTrue();
    assertThat(value.get()).isEqualTo(leafValue);
    // check import with incorrect key: TrieNodeResult.INVALID_KEY
    assertThat(repository.importTrieNode(smallNodeKey, branchValue, DatabaseType.STATE)).isEqualTo(TrieNodeResult.INVALID_KEY);
    value = db.get(smallNodeKey);
    assertThat(value.isPresent()).isFalse();
    // check import with incorrect value: TrieNodeResult.INVALID_VALUE
    assertThat(repository.importTrieNode(altNodeKey, emptyValue, DatabaseType.STATE)).isEqualTo(TrieNodeResult.INVALID_VALUE);
    value = db.get(altNodeKey);
    assertThat(value.isPresent()).isFalse();
}
Also used : ByteArrayKeyValueDatabase(org.aion.db.impl.ByteArrayKeyValueDatabase) Test(org.junit.Test)

Example 13 with ByteArrayKeyValueDatabase

use of org.aion.db.impl.ByteArrayKeyValueDatabase in project aion by aionnetwork.

the class AvmContractDetailsTest 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);
    byte[] storageHash = RandomUtils.nextBytes(32);
    byte[] graphHash = RandomUtils.nextBytes(32);
    byte[] graphBytes = RandomUtils.nextBytes(100);
    when(mockDatabase.get(rootHash)).thenReturn(Optional.of(RLP.encodeList(RLP.encodeElement(storageHash), RLP.encodeElement(graphHash))));
    when(mockDatabase.get(graphHash)).thenReturn(Optional.of(graphBytes));
    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);
    AvmContractDetails details = AvmContractDetails.decodeAtRoot(input, db, mockDatabase, rootHash);
    assertThat(details.address).isEqualTo(address);
    // because it uses the setCodes method
    assertThat(details.isDirty()).isTrue();
    assertThat(details.isDeleted()).isFalse();
    assertThat(details.getObjectGraph()).isEqualTo(graphBytes);
    assertThat(details.getCodes().size()).isEqualTo(1);
    assertThat(details.getCodes().values()).contains(ByteArrayWrapper.wrap(codeBytes));
    assertThat(details.getCode(h256(codeBytes))).isEqualTo(codeBytes);
    storageHash = trie.getRootHash();
    byte[] concatenated = new byte[storageHash.length + graphHash.length];
    System.arraycopy(storageHash, 0, concatenated, 0, storageHash.length);
    System.arraycopy(graphHash, 0, concatenated, storageHash.length, graphHash.length);
    assertThat(details.getStorageHash()).isEqualTo(h256(concatenated));
    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)

Example 14 with ByteArrayKeyValueDatabase

use of org.aion.db.impl.ByteArrayKeyValueDatabase in project aion by aionnetwork.

the class AvmContractDetailsTest method testDecode_withInLineStorageAndEmptyStorageTrie.

@Test
public void testDecode_withInLineStorageAndEmptyStorageTrie() {
    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);
    byte[] storageHash = EMPTY_TRIE_HASH;
    byte[] graphHash = RandomUtils.nextBytes(32);
    byte[] graphBytes = RandomUtils.nextBytes(100);
    when(mockDatabase.get(rootHash)).thenReturn(Optional.of(RLP.encodeList(RLP.encodeElement(storageHash), RLP.encodeElement(graphHash))));
    when(mockDatabase.get(graphHash)).thenReturn(Optional.of(graphBytes));
    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);
    AvmContractDetails details = AvmContractDetails.decodeAtRoot(input, db, mockDatabase, rootHash);
    assertThat(details.address).isEqualTo(address);
    // because it uses the setCodes method
    assertThat(details.isDirty()).isTrue();
    assertThat(details.isDeleted()).isFalse();
    assertThat(details.getObjectGraph()).isEqualTo(graphBytes);
    assertThat(details.getCodes().size()).isEqualTo(1);
    assertThat(details.getCodes().values()).contains(ByteArrayWrapper.wrap(codeBytes));
    assertThat(details.getCode(h256(codeBytes))).isEqualTo(codeBytes);
    byte[] concatenated = new byte[storageHash.length + graphHash.length];
    System.arraycopy(storageHash, 0, concatenated, 0, storageHash.length);
    System.arraycopy(graphHash, 0, concatenated, storageHash.length, graphHash.length);
    assertThat(details.getStorageHash()).isEqualTo(h256(concatenated));
    for (ByteArrayWrapper key : storage.keySet()) {
        assertThat(details.get(key)).isNull();
    }
    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)

Example 15 with ByteArrayKeyValueDatabase

use of org.aion.db.impl.ByteArrayKeyValueDatabase in project aion by aionnetwork.

the class DatabaseUtils method connectAndOpen.

public static ByteArrayKeyValueDatabase connectAndOpen(Properties info, Logger LOG) {
    // get the database object
    ByteArrayKeyValueDatabase db = DatabaseFactory.connect(info, LOG);
    // open the database connection
    db.open();
    return db;
}
Also used : ByteArrayKeyValueDatabase(org.aion.db.impl.ByteArrayKeyValueDatabase)

Aggregations

ByteArrayKeyValueDatabase (org.aion.db.impl.ByteArrayKeyValueDatabase)20 Test (org.junit.Test)15 Properties (java.util.Properties)6 MockDB (org.aion.db.impl.mockdb.MockDB)6 AionAddress (org.aion.types.AionAddress)5 ByteArrayWrapper (org.aion.util.types.ByteArrayWrapper)5 SecureTrie (org.aion.zero.impl.trie.SecureTrie)5 HashMap (java.util.HashMap)4 RLPElement (org.aion.rlp.RLPElement)4 RLPContractDetails (org.aion.zero.impl.db.DetailsDataStore.RLPContractDetails)4 Logger (org.slf4j.Logger)3 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)2 Block (org.aion.zero.impl.types.Block)2 MiningBlock (org.aion.zero.impl.types.MiningBlock)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AionTransaction (org.aion.base.AionTransaction)1 ImportResult (org.aion.zero.impl.core.ImportResult)1 AionBlockStore (org.aion.zero.impl.db.AionBlockStore)1 BlockInfo (org.aion.zero.impl.db.AionBlockStore.BlockInfo)1