Search in sources :

Example 96 with ByteArrayWrapper

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

the class AionTxInfo method decodeToTxInfo.

private static AionTxInfo decodeToTxInfo(SharedRLPList rlpTxInfo) {
    Objects.requireNonNull(rlpTxInfo);
    AionTxReceipt receipt = new AionTxReceipt((SharedRLPList) rlpTxInfo.get(INDEX_RECEIPT));
    ByteArrayWrapper blockHash = ByteArrayWrapper.wrap(rlpTxInfo.get(INDEX_BLOCK_HASH).getRLPData());
    int index;
    byte[] txIndex = rlpTxInfo.get(INDEX_TX_INDEX).getRLPData();
    if (txIndex == null) {
        index = 0;
    } else {
        index = new BigInteger(1, txIndex).intValue();
    }
    boolean createdWithInternalTx;
    List<InternalTransaction> internalTransactions;
    switch(rlpTxInfo.size()) {
        case SIZE_OF_OLD_ENCODING:
            // old encodings are incomplete since internal tx were not stored
            createdWithInternalTx = false;
            internalTransactions = null;
            break;
        case SIZE_WITH_BASE_DATA:
            // read the completeness flag from storage
            createdWithInternalTx = rlpTxInfo.get(INDEX_CREATE_FLAG).getRLPData().length == 1;
            internalTransactions = null;
            break;
        case SIZE_WITH_INTERNAL_TRANSACTIONS:
            // read the completeness flag from storage
            createdWithInternalTx = rlpTxInfo.get(INDEX_CREATE_FLAG).getRLPData().length == 1;
            // decode the internal transactions
            internalTransactions = new ArrayList<>();
            SharedRLPList internalTxRlp = (SharedRLPList) rlpTxInfo.get(INDEX_INTERNAL_TX);
            for (RLPElement item : internalTxRlp) {
                internalTransactions.add(fromRlp((SharedRLPList) item));
            }
            break;
        default:
            // incorrect encoding
            return null;
    }
    return new AionTxInfo(receipt, blockHash, index, internalTransactions, createdWithInternalTx);
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) RLPElement(org.aion.rlp.RLPElement) BigInteger(java.math.BigInteger) AionTxReceipt(org.aion.base.AionTxReceipt) InternalTransaction(org.aion.types.InternalTransaction) SharedRLPList(org.aion.rlp.SharedRLPList)

Example 97 with ByteArrayWrapper

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

the class Cache method setDB.

@SuppressWarnings("OptionalGetWithoutIsPresent")
public void setDB(ByteArrayKeyValueStore kvds) {
    if (this.dataSource == kvds) {
        return;
    }
    Map<byte[], byte[]> rows = new HashMap<>();
    if (this.dataSource == null) {
        for (ByteArrayWrapper key : nodes.keySet()) {
            Node node = nodes.get(key);
            if (node == null) {
                rows.put(key.toBytes(), null);
            } else if (!node.isDirty()) {
                rows.put(key.toBytes(), node.getValue().encode());
            }
        }
    } else {
        Iterator<byte[]> iterator = dataSource.keys();
        while (iterator.hasNext()) {
            byte[] key = iterator.next();
            rows.put(key, this.dataSource.get(key).get());
        }
        try {
            this.dataSource.close();
        } catch (Exception e) {
            LOG.error("Unable to close data source.", e);
        }
    }
    kvds.putBatch(rows);
    this.dataSource = kvds;
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 98 with ByteArrayWrapper

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

the class Cache method delete.

public void delete(byte[] key) {
    ByteArrayWrapper wrappedKey = wrap(key);
    this.nodes.remove(wrappedKey);
    if (dataSource != null) {
        this.dataSource.delete(key);
    }
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper)

Example 99 with ByteArrayWrapper

use of org.aion.util.types.ByteArrayWrapper 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 100 with ByteArrayWrapper

use of org.aion.util.types.ByteArrayWrapper 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)

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