Search in sources :

Example 11 with ByteArrayWrapper

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

the class InnerContractDetailsTest method testCommitToStored_withStorageOnFvm.

@Test
public void testCommitToStored_withStorageOnFvm() {
    AionAddress address = mock(AionAddress.class);
    ByteArrayKeyValueStore db = mock(XorDataSource.class);
    FvmContractDetails parent = new FvmContractDetails(address, db);
    InnerContractDetails child = new InnerContractDetails(null);
    Map<ByteArrayWrapper, ByteArrayWrapper> storage = new HashMap<>();
    for (int i = 0; i < 3; i++) {
        ByteArrayWrapper key = ByteArrayWrapper.wrap(RandomUtils.nextBytes(32));
        ByteArrayWrapper value = ByteArrayWrapper.wrap(RandomUtils.nextBytes(100));
        child.put(key, value);
        storage.put(key, value);
    }
    ByteArrayWrapper deletedKey = ByteArrayWrapper.wrap(RandomUtils.nextBytes(32));
    child.delete(deletedKey);
    storage.put(deletedKey, null);
    assertThat(child.isDirty()).isTrue();
    assertThat(parent.getVmType()).isEqualTo(InternalVmType.FVM);
    child.commitTo(parent);
    assertThat(parent.getVmType()).isEqualTo(InternalVmType.FVM);
    for (ByteArrayWrapper key : storage.keySet()) {
        assertThat(parent.get(key)).isEqualTo(storage.get(key));
    }
    assertThat(parent.isDirty()).isTrue();
}
Also used : AionAddress(org.aion.types.AionAddress) ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) HashMap(java.util.HashMap) ByteArrayKeyValueStore(org.aion.db.impl.ByteArrayKeyValueStore) Test(org.junit.Test)

Example 12 with ByteArrayWrapper

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

the class InnerContractDetailsTest method testCommitToInner_withStorage.

@Test
public void testCommitToInner_withStorage() {
    InnerContractDetails parent = new InnerContractDetails(null);
    InnerContractDetails child = new InnerContractDetails(null);
    Map<ByteArrayWrapper, ByteArrayWrapper> storage = new HashMap<>();
    for (int i = 0; i < 3; i++) {
        ByteArrayWrapper key = ByteArrayWrapper.wrap(RandomUtils.nextBytes(32));
        ByteArrayWrapper value = ByteArrayWrapper.wrap(RandomUtils.nextBytes(100));
        child.put(key, value);
        storage.put(key, value);
    }
    ByteArrayWrapper deletedKey = ByteArrayWrapper.wrap(RandomUtils.nextBytes(32));
    child.delete(deletedKey);
    storage.put(deletedKey, null);
    assertThat(child.isDirty()).isTrue();
    assertThat(parent.getVmType()).isEqualTo(InternalVmType.EITHER);
    child.commitTo(parent);
    assertThat(parent.getVmType()).isEqualTo(InternalVmType.EITHER);
    for (ByteArrayWrapper key : storage.keySet()) {
        assertThat(parent.get(key)).isEqualTo(storage.get(key));
    }
    assertThat(parent.isDirty()).isTrue();
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 13 with ByteArrayWrapper

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

the class PendingBlockStoreTest method testDropPendingQueues_wException.

@Test
public void testDropPendingQueues_wException() {
    Properties props = new Properties();
    props.setProperty(Props.DB_TYPE, DBVendor.MOCKDB.toValue());
    PendingBlockStore pb = null;
    try {
        pb = new PendingBlockStore(props);
    } catch (IOException e) {
        e.printStackTrace();
    }
    assertThat(pb.isOpen()).isTrue();
    // add first queue
    List<Block> blocks = TestResources.consecutiveBlocks(6);
    Block first = blocks.get(0);
    pb.addBlockRange(blocks);
    // add second queue
    MiningBlock altBlock = (MiningBlock) BlockUtil.newBlockFromRlp(first.getEncoded());
    MiningBlockHeader newHeader = MiningBlockHeader.Builder.newInstance().withHeader(altBlock.getHeader()).withExtraData("random".getBytes()).build();
    altBlock.updateHeader(newHeader);
    List<Block> sideChain = new ArrayList<>();
    sideChain.add(altBlock);
    pb.addBlockRange(sideChain);
    // check storage updates
    assertThat(pb.getIndexSize()).isEqualTo(7);
    assertThat(pb.getLevelSize()).isEqualTo(1);
    assertThat(pb.getQueueSize()).isEqualTo(2);
    // closing the pending block store to cause exception
    pb.close();
    // test drop functionality
    Map<ByteArrayWrapper, List<Block>> actual = pb.loadBlockRange(first.getNumber());
    pb.dropPendingQueues(first.getNumber(), actual.keySet(), actual);
}
Also used : MiningBlockHeader(org.aion.zero.impl.types.MiningBlockHeader) ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) ArrayList(java.util.ArrayList) MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) Properties(java.util.Properties) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Example 14 with ByteArrayWrapper

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

the class AionRepositoryCacheTest method testGetStorageValueIsSingleZero.

@Test
public void testGetStorageValueIsSingleZero() {
    AionAddress address = getNewAddress();
    ByteArrayWrapper key = ByteArrayWrapper.wrap(RandomUtils.nextBytes(SINGLE_BYTES));
    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 15 with ByteArrayWrapper

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

the class ContractInformationTest method testDecodeEncode_correctMultiInstance.

@Test
public void testDecodeEncode_correctMultiInstance() {
    byte[] encoding = // contract info
    RLP.encodeList(// hash map entry 1
    RLP.encodeList(encodedHash1, RLP.encodeList(RLP.encodeList(RLP.encodeList(encodedHash2, flag1)), avm)), // hash map entry 2
    RLP.encodeList(encodedHash2, RLP.encodeList(RLP.encodeList(RLP.encodeList(encodedHash1, flag1), RLP.encodeList(encodedHash2, flag0)), fvm)));
    ContractInformation ci = ContractInformation.RLP_SERIALIZER.deserialize(encoding);
    System.out.println(ci);
    assertThat(ci).isNotNull();
    assertThat(ci.getVmUsed(hash1)).isEqualTo(InternalVmType.AVM);
    assertThat(ci.getVmUsed(hash2)).isEqualTo(InternalVmType.FVM);
    assertThat(ci.getVmUsed(new byte[] { 1, 2 })).isEqualTo(InternalVmType.UNKNOWN);
    assertThat(ci.getInceptionBlocks(hash1)).isEqualTo(Set.of(ByteArrayWrapper.wrap(hash2)));
    assertThat(ci.getInceptionBlocks(hash2)).isEqualTo(Set.of(ByteArrayWrapper.wrap(hash1), ByteArrayWrapper.wrap(hash2)));
    assertThat(ci.getInceptionBlocks(new byte[HASH_SIZE]).isEmpty()).isTrue();
    assertThat(ci.isComplete(hash1, hash2)).isTrue();
    assertThat(ci.isComplete(hash1, hash1)).isFalse();
    assertThat(ci.isComplete(hash2, hash1)).isTrue();
    assertThat(ci.isComplete(hash2, hash2)).isFalse();
    assertThat(ci.isComplete(new byte[HASH_SIZE], hash2)).isFalse();
    // check that the encoding matches
    // note that this might differ if the hash1 would not be less than hash2
    assertThat(ContractInformation.RLP_SERIALIZER.serialize(ci)).isEqualTo(encoding);
    // constructor version
    ByteArrayWrapper key1 = ByteArrayWrapper.wrap(hash1);
    ByteArrayWrapper key2 = ByteArrayWrapper.wrap(hash2);
    ci = new ContractInformation(key1, InternalVmType.AVM, key2, true);
    ci.append(key2, InternalVmType.FVM, key1, true);
    ci.append(key2, InternalVmType.FVM, key2, false);
    assertThat(ContractInformation.RLP_SERIALIZER.serialize(ci)).isEqualTo(encoding);
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) 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