Search in sources :

Example 1 with DataWord

use of org.aion.mcf.vm.types.DataWord in project aion by aionnetwork.

the class AbstractRepositoryCache method createAccount.

@Override
public synchronized AccountState createAccount(Address address) {
    AccountState accountState = new AccountState();
    cachedAccounts.put(address, accountState);
    // TODO: unify contract details initialization from Impl and Track
    IContractDetails<DataWord> contractDetails = new ContractDetailsCacheImpl(null);
    // TODO: refactor to use makeDirty() from AbstractState
    contractDetails.setDirty(true);
    cachedDetails.put(address, contractDetails);
    return accountState;
}
Also used : DataWord(org.aion.mcf.vm.types.DataWord) AccountState(org.aion.mcf.core.AccountState)

Example 2 with DataWord

use of org.aion.mcf.vm.types.DataWord in project aion by aionnetwork.

the class ContractDetailsCacheImpl method getStorageHash.

@Override
public byte[] getStorageHash() {
    // todo: unsupported
    SecureTrie storageTrie = new SecureTrie(null);
    for (DataWord key : storage.keySet()) {
        DataWord value = storage.get(key);
        storageTrie.update(key.getData(), RLP.encodeElement(value.getNoLeadZeroesData()));
    }
    return storageTrie.getRootHash();
}
Also used : DataWord(org.aion.mcf.vm.types.DataWord) SecureTrie(org.aion.mcf.trie.SecureTrie)

Example 3 with DataWord

use of org.aion.mcf.vm.types.DataWord in project aion by aionnetwork.

the class DetailsDataStore method get.

/**
 * Fetches the ContractDetails from the cache, and if it doesn't exist, add
 * to the remove set.
 *
 * @param key
 * @return
 */
public synchronized IContractDetails<DataWord> get(byte[] key) {
    ByteArrayWrapper wrappedKey = wrap(key);
    Optional<byte[]> rawDetails = detailsSrc.get(key);
    // If it doesn't exist in cache or database.
    if (!rawDetails.isPresent()) {
        // If it isn't in removes set, we add it to removes set.
        if (!removes.contains(wrappedKey)) {
            removes.add(wrappedKey);
        }
        return null;
    }
    // Found something from cache or database, return it by decoding it.
    IContractDetails<DataWord> detailsImpl = repoConfig.contractDetailsImpl();
    detailsImpl.setDataSource(storageSrc);
    // We can safely get as we checked
    detailsImpl.decode(rawDetails.get());
    return detailsImpl;
}
Also used : ByteArrayWrapper(org.aion.base.util.ByteArrayWrapper) DataWord(org.aion.mcf.vm.types.DataWord)

Example 4 with DataWord

use of org.aion.mcf.vm.types.DataWord in project aion by aionnetwork.

the class AionContractDetailsTest method testExternalStorageSerialization.

@Test
public void testExternalStorageSerialization() {
    Address address = Address.wrap(RandomUtils.nextBytes(Address.ADDRESS_LEN));
    byte[] code = RandomUtils.nextBytes(512);
    Map<DataWord, DataWord> elements = new HashMap<>();
    AionRepositoryImpl repository = AionRepositoryImpl.createForTesting(repoConfig);
    IByteArrayKeyValueDatabase externalStorage = repository.getDetailsDatabase();
    AionContractDetailsImpl original = new AionContractDetailsImpl(0, 1000000);
    original.setExternalStorageDataSource(externalStorage);
    original.setAddress(address);
    original.setCode(code);
    original.externalStorage = true;
    for (int i = 0; i < IN_MEMORY_STORAGE_LIMIT / 64 + 10; i++) {
        DataWord key = new DataWord(RandomUtils.nextBytes(16));
        DataWord value = new DataWord(RandomUtils.nextBytes(16));
        elements.put(key, value);
        original.put(key, value);
    }
    original.syncStorage();
    byte[] rlp = original.getEncoded();
    AionContractDetailsImpl deserialized = new AionContractDetailsImpl();
    deserialized.setExternalStorageDataSource(externalStorage);
    deserialized.decode(rlp);
    assertEquals(deserialized.externalStorage, true);
    assertTrue(address.equals(deserialized.getAddress()));
    assertEquals(ByteUtil.toHexString(code), ByteUtil.toHexString(deserialized.getCode()));
    Map<DataWord, DataWord> storage = deserialized.getStorage();
    assertEquals(elements.size(), storage.size());
    for (DataWord key : elements.keySet()) {
        assertEquals(elements.get(key), storage.get(key));
    }
    DataWord deletedKey = elements.keySet().iterator().next();
    deserialized.put(deletedKey, DataWord.ZERO);
    deserialized.put(new DataWord(RandomUtils.nextBytes(16)), DataWord.ZERO);
}
Also used : Address(org.aion.base.type.Address) IByteArrayKeyValueDatabase(org.aion.base.db.IByteArrayKeyValueDatabase) HashMap(java.util.HashMap) DataWord(org.aion.mcf.vm.types.DataWord) AionRepositoryImpl(org.aion.zero.impl.db.AionRepositoryImpl) AionContractDetailsImpl(org.aion.zero.db.AionContractDetailsImpl) Test(org.junit.Test)

Example 5 with DataWord

use of org.aion.mcf.vm.types.DataWord in project aion by aionnetwork.

the class AionContractDetailsTest method test_2.

@Test
public void test_2() throws Exception {
    byte[] code = ByteUtil.hexStringToBytes("7c0100000000000000000000000000000000000000000000000000000000600035046333d546748114610065578063430fe5f01461007c5780634d432c1d1461008d578063501385b2146100b857806357eb3b30146100e9578063dbc7df61146100fb57005b6100766004356024356044356102f0565b60006000f35b61008760043561039e565b60006000f35b610098600435610178565b8073ffffffffffffffffffffffffffffffffffffffff1660005260206000f35b6100c96004356024356044356101a0565b8073ffffffffffffffffffffffffffffffffffffffff1660005260206000f35b6100f1610171565b8060005260206000f35b610106600435610133565b8360005282602052816040528073ffffffffffffffffffffffffffffffffffffffff1660605260806000f35b5b60006020819052908152604090208054600182015460028301546003909301549192909173ffffffffffffffffffffffffffffffffffffffff1684565b5b60015481565b5b60026020526000908152604090205473ffffffffffffffffffffffffffffffffffffffff1681565b73ffffffffffffffffffffffffffffffffffffffff831660009081526020819052604081206002015481908302341080156101fe575073ffffffffffffffffffffffffffffffffffffffff8516600090815260208190526040812054145b8015610232575073ffffffffffffffffffffffffffffffffffffffff85166000908152602081905260409020600101548390105b61023b57610243565b3391506102e8565b6101966103ca60003973ffffffffffffffffffffffffffffffffffffffff3381166101965285166101b68190526000908152602081905260408120600201546101d6526101f68490526102169080f073ffffffffffffffffffffffffffffffffffffffff8616600090815260208190526040902060030180547fffffffffffffffffffffffff0000000000000000000000000000000000000000168217905591508190505b509392505050565b73ffffffffffffffffffffffffffffffffffffffff33166000908152602081905260408120548190821461032357610364565b60018054808201909155600090815260026020526040902080547fffffffffffffffffffffffff000000000000000000000000000000000000000016331790555b50503373ffffffffffffffffffffffffffffffffffffffff1660009081526020819052604090209081556001810192909255600290910155565b3373ffffffffffffffffffffffffffffffffffffffff166000908152602081905260409020600201555600608061019660043960048051602451604451606451600080547fffffffffffffffffffffffff0000000000000000000000000000000000000000908116909517815560018054909516909317909355600355915561013390819061006390396000f3007c0100000000000000000000000000000000000000000000000000000000600035046347810fe381146100445780637e4a1aa81461005557806383d2421b1461006957005b61004f6004356100ab565b60006000f35b6100636004356024356100fc565b60006000f35b61007460043561007a565b60006000f35b6001543373ffffffffffffffffffffffffffffffffffffffff9081169116146100a2576100a8565b60078190555b50565b73ffffffffffffffffffffffffffffffffffffffff8116600090815260026020526040902080547fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0016600117905550565b6001543373ffffffffffffffffffffffffffffffffffffffff9081169116146101245761012f565b600582905560068190555b505056");
    Address address = Address.wrap(RandomUtils.nextBytes(Address.ADDRESS_LEN));
    byte[] key_0 = ByteUtil.hexStringToBytes("18d63b70aa690ad37cb50908746c9a55");
    byte[] val_0 = ByteUtil.hexStringToBytes("00000000000000000000000000000064");
    byte[] key_1 = ByteUtil.hexStringToBytes("18d63b70aa690ad37cb50908746c9a56");
    byte[] val_1 = ByteUtil.hexStringToBytes("0000000000000000000000000000000c");
    byte[] key_2 = ByteUtil.hexStringToBytes("5a448d1967513482947d1d3f6104316f");
    byte[] val_2 = ByteUtil.hexStringToBytes("00000000000000000000000000000000");
    byte[] key_3 = ByteUtil.hexStringToBytes("5a448d1967513482947d1d3f61043171");
    byte[] val_3 = ByteUtil.hexStringToBytes("00000000000000000000000000000014");
    byte[] key_4 = ByteUtil.hexStringToBytes("18d63b70aa690ad37cb50908746c9a54");
    byte[] val_4 = ByteUtil.hexStringToBytes("00000000000000000000000000000000");
    byte[] key_5 = ByteUtil.hexStringToBytes("5a448d1967513482947d1d3f61043170");
    byte[] val_5 = ByteUtil.hexStringToBytes("00000000000000000000000000000078");
    byte[] key_6 = ByteUtil.hexStringToBytes("c83a08bbccc01a0644d599ccd2a7c2e0");
    byte[] val_6 = ByteUtil.hexStringToBytes("8fbec874791c4e3f9f48a59a44686efe");
    byte[] key_7 = ByteUtil.hexStringToBytes("5aa541c6c03f602a426f04ae47508bb8");
    byte[] val_7 = ByteUtil.hexStringToBytes("7a657031000000000000000000000000");
    byte[] key_8 = ByteUtil.hexStringToBytes("5aa541c6c03f602a426f04ae47508bb9");
    byte[] val_8 = ByteUtil.hexStringToBytes("000000000000000000000000000000c8");
    byte[] key_9 = ByteUtil.hexStringToBytes("5aa541c6c03f602a426f04ae47508bba");
    byte[] val_9 = ByteUtil.hexStringToBytes("0000000000000000000000000000000a");
    byte[] key_10 = ByteUtil.hexStringToBytes("00000000000000000000000000000001");
    byte[] val_10 = ByteUtil.hexStringToBytes("00000000000000000000000000000003");
    byte[] key_11 = ByteUtil.hexStringToBytes("5aa541c6c03f602a426f04ae47508bbb");
    byte[] val_11 = ByteUtil.hexStringToBytes("194bcfc3670d8a1613e5b0c790036a35");
    byte[] key_12 = ByteUtil.hexStringToBytes("aee92919b8c3389af86ef24535e8a28c");
    byte[] val_12 = ByteUtil.hexStringToBytes("cfe293a85bef5915e1a7acb37bf0c685");
    byte[] key_13 = ByteUtil.hexStringToBytes("65c996598dc972688b7ace676c89077b");
    byte[] val_13 = ByteUtil.hexStringToBytes("d6ee27e285f2de7b68e8db25cf1b1063");
    AionContractDetailsImpl contractDetails = new AionContractDetailsImpl();
    contractDetails.setCode(code);
    contractDetails.setAddress(address);
    contractDetails.put(new DataWord(key_0), new DataWord(val_0));
    contractDetails.put(new DataWord(key_1), new DataWord(val_1));
    contractDetails.put(new DataWord(key_2), new DataWord(val_2));
    contractDetails.put(new DataWord(key_3), new DataWord(val_3));
    contractDetails.put(new DataWord(key_4), new DataWord(val_4));
    contractDetails.put(new DataWord(key_5), new DataWord(val_5));
    contractDetails.put(new DataWord(key_6), new DataWord(val_6));
    contractDetails.put(new DataWord(key_7), new DataWord(val_7));
    contractDetails.put(new DataWord(key_8), new DataWord(val_8));
    contractDetails.put(new DataWord(key_9), new DataWord(val_9));
    contractDetails.put(new DataWord(key_10), new DataWord(val_10));
    contractDetails.put(new DataWord(key_11), new DataWord(val_11));
    contractDetails.put(new DataWord(key_12), new DataWord(val_12));
    contractDetails.put(new DataWord(key_13), new DataWord(val_13));
    byte[] data = contractDetails.getEncoded();
    AionContractDetailsImpl contractDetails_ = new AionContractDetailsImpl(data);
    assertEquals(ByteUtil.toHexString(code), ByteUtil.toHexString(contractDetails_.getCode()));
    assertTrue(address.equals(contractDetails_.getAddress()));
    assertEquals(ByteUtil.toHexString(val_1), ByteUtil.toHexString(contractDetails_.get(new DataWord(key_1)).getData()));
    assertEquals(ByteUtil.toHexString(val_2), ByteUtil.toHexString(contractDetails_.get(new DataWord(key_2)).getData()));
    assertEquals(ByteUtil.toHexString(val_3), ByteUtil.toHexString(contractDetails_.get(new DataWord(key_3)).getData()));
    assertEquals(ByteUtil.toHexString(val_4), ByteUtil.toHexString(contractDetails_.get(new DataWord(key_4)).getData()));
    assertEquals(ByteUtil.toHexString(val_5), ByteUtil.toHexString(contractDetails_.get(new DataWord(key_5)).getData()));
    assertEquals(ByteUtil.toHexString(val_6), ByteUtil.toHexString(contractDetails_.get(new DataWord(key_6)).getData()));
    assertEquals(ByteUtil.toHexString(val_7), ByteUtil.toHexString(contractDetails_.get(new DataWord(key_7)).getData()));
    assertEquals(ByteUtil.toHexString(val_8), ByteUtil.toHexString(contractDetails_.get(new DataWord(key_8)).getData()));
    assertEquals(ByteUtil.toHexString(val_9), ByteUtil.toHexString(contractDetails_.get(new DataWord(key_9)).getData()));
    assertEquals(ByteUtil.toHexString(val_10), ByteUtil.toHexString(contractDetails_.get(new DataWord(key_10)).getData()));
    assertEquals(ByteUtil.toHexString(val_11), ByteUtil.toHexString(contractDetails_.get(new DataWord(key_11)).getData()));
    assertEquals(ByteUtil.toHexString(val_12), ByteUtil.toHexString(contractDetails_.get(new DataWord(key_12)).getData()));
    assertEquals(ByteUtil.toHexString(val_13), ByteUtil.toHexString(contractDetails_.get(new DataWord(key_13)).getData()));
}
Also used : Address(org.aion.base.type.Address) DataWord(org.aion.mcf.vm.types.DataWord) AionContractDetailsImpl(org.aion.zero.db.AionContractDetailsImpl) Test(org.junit.Test)

Aggregations

DataWord (org.aion.mcf.vm.types.DataWord)29 Address (org.aion.base.type.Address)11 AccountState (org.aion.mcf.core.AccountState)11 Test (org.junit.Test)9 AionContractDetailsImpl (org.aion.zero.db.AionContractDetailsImpl)5 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)5 HashMap (java.util.HashMap)3 IByteArrayKeyValueDatabase (org.aion.base.db.IByteArrayKeyValueDatabase)3 IRepositoryCache (org.aion.base.db.IRepositoryCache)3 ByteArrayWrapper (org.aion.base.util.ByteArrayWrapper)3 ContractDetailsCacheImpl (org.aion.mcf.db.ContractDetailsCacheImpl)3 IContractDetails (org.aion.base.db.IContractDetails)2 RLPElement (org.aion.rlp.RLPElement)2 RLPList (org.aion.rlp.RLPList)2 AionTransaction (org.aion.zero.types.AionTransaction)2 BigInteger (java.math.BigInteger)1 Map (java.util.Map)1 ByteUtil.toHexString (org.aion.base.util.ByteUtil.toHexString)1 IEvent (org.aion.evtmgr.IEvent)1 EventBlock (org.aion.evtmgr.impl.evt.EventBlock)1