Search in sources :

Example 21 with DataWord

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

the class AionContractDetailsTest method test_1.

@Test
public void test_1() throws Exception {
    byte[] code = ByteUtil.hexStringToBytes("60016002");
    byte[] key_1 = ByteUtil.hexStringToBytes("111111");
    byte[] val_1 = ByteUtil.hexStringToBytes("aaaaaa");
    byte[] key_2 = ByteUtil.hexStringToBytes("222222");
    byte[] val_2 = ByteUtil.hexStringToBytes("bbbbbb");
    AionContractDetailsImpl contractDetails = new AionContractDetailsImpl(// CfgAion.inst().getDb().getPrune(),
    -1, // CfgAion.inst().getDb().getDetailsInMemoryStorageLimit()
    1000000);
    contractDetails.setCode(code);
    contractDetails.put(new DataWord(key_1), new DataWord(val_1));
    contractDetails.put(new DataWord(key_2), new DataWord(val_2));
    byte[] data = contractDetails.getEncoded();
    AionContractDetailsImpl contractDetails_ = new AionContractDetailsImpl(data);
    assertEquals(ByteUtil.toHexString(code), ByteUtil.toHexString(contractDetails_.getCode()));
    assertEquals(ByteUtil.toHexString(val_1), ByteUtil.toHexString(contractDetails_.get(new DataWord(key_1)).getNoLeadZeroesData()));
    assertEquals(ByteUtil.toHexString(val_2), ByteUtil.toHexString(contractDetails_.get(new DataWord(key_2)).getNoLeadZeroesData()));
}
Also used : DataWord(org.aion.mcf.vm.types.DataWord) AionContractDetailsImpl(org.aion.zero.db.AionContractDetailsImpl) Test(org.junit.Test)

Example 22 with DataWord

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

the class AionRepositoryImplTest method testRepoTrackUpdateStorageRow.

/**
 * Repo track test suite
 */
/**
 * This test confirms that updates done on the repo track are successfully translated
 * into the root repository.
 */
@Test
public void testRepoTrackUpdateStorageRow() {
    final AionRepositoryImpl repository = AionRepositoryImpl.createForTesting(repoConfig);
    final IRepositoryCache<AccountState, DataWord, IBlockStoreBase<?, ?>> repoTrack = repository.startTracking();
    final Address defaultAccount = Address.wrap(ByteUtil.hexStringToBytes("CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3"));
    final byte[] key = HashUtil.blake128("hello".getBytes());
    final byte[] value = HashUtil.blake128("world".getBytes());
    repoTrack.addBalance(defaultAccount, BigInteger.valueOf(1));
    final byte[] originalRoot = repository.getRoot();
    repoTrack.addStorageRow(defaultAccount, new DataWord(key), new DataWord(value));
    DataWord retrievedStorageValue = repoTrack.getStorageValue(defaultAccount, new DataWord(key));
    assertThat(retrievedStorageValue).isEqualTo(new DataWord(value));
    // commit changes, then check that the root has updated
    repoTrack.flush();
    assertThat(repository.getStorageValue(defaultAccount, new DataWord(key))).isEqualTo(retrievedStorageValue);
    final byte[] newRoot = repository.getRoot();
    assertThat(newRoot).isNotEqualTo(originalRoot);
}
Also used : Address(org.aion.base.type.Address) AionRepositoryImpl(org.aion.zero.impl.db.AionRepositoryImpl) DataWord(org.aion.mcf.vm.types.DataWord) AccountState(org.aion.mcf.core.AccountState) IBlockStoreBase(org.aion.mcf.db.IBlockStoreBase) Test(org.junit.Test)

Example 23 with DataWord

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

the class AionRepositoryImplTest method testAccountStateUpdateStorageRow.

@Test
public void testAccountStateUpdateStorageRow() {
    AionRepositoryImpl repository = AionRepositoryImpl.createForTesting(repoConfig);
    IRepositoryCache track = repository.startTracking();
    Address defaultAccount = Address.wrap(ByteUtil.hexStringToBytes("CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3"));
    track.addBalance(defaultAccount, BigInteger.valueOf(1));
    // Consider the original root the one after an account has been added
    byte[] originalRoot = repository.getRoot();
    byte[] key = HashUtil.blake128("hello".getBytes());
    byte[] value = HashUtil.blake128("world".getBytes());
    track.addStorageRow(defaultAccount, new DataWord(key), new DataWord(value));
    track.flush();
    byte[] retrievedValue = repository.getStorageValue(defaultAccount, new DataWord(key)).getNoLeadZeroesData();
    assertThat(retrievedValue).isEqualTo(value);
    byte[] newRoot = repository.getRoot();
    System.out.println(String.format("original root: %s", ByteUtil.toHexString(originalRoot)));
    System.out.println(String.format("new root: %s", ByteUtil.toHexString(newRoot)));
}
Also used : Address(org.aion.base.type.Address) IRepositoryCache(org.aion.base.db.IRepositoryCache) AionRepositoryImpl(org.aion.zero.impl.db.AionRepositoryImpl) DataWord(org.aion.mcf.vm.types.DataWord) Test(org.junit.Test)

Example 24 with DataWord

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

the class AionRepositoryImplTest method testAccountStateUpdateStorageRowFlush.

@Test
public void testAccountStateUpdateStorageRowFlush() {
    AionRepositoryImpl repository = AionRepositoryImpl.createForTesting(repoConfig);
    IRepositoryCache track = repository.startTracking();
    Address defaultAccount = Address.wrap(ByteUtil.hexStringToBytes("CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3CAF3"));
    track.addBalance(defaultAccount, BigInteger.valueOf(1));
    // Consider the original root the one after an account has been added
    byte[] originalRoot = repository.getRoot();
    byte[] key = HashUtil.blake128("hello".getBytes());
    byte[] value = HashUtil.blake128("world".getBytes());
    track.addStorageRow(defaultAccount, new DataWord(key), new DataWord(value));
    // does not call parent's flush
    track.flush();
    repository.flush();
    /**
     * Verify that the account has been flushed
     */
    IByteArrayKeyValueDatabase detailsDB = repository.getDetailsDatabase();
    Optional<byte[]> serializedDetails = detailsDB.get(defaultAccount.toBytes());
    assertThat(serializedDetails.isPresent()).isEqualTo(true);
    AionContractDetailsImpl details = new AionContractDetailsImpl(0, 1000000);
    details.decode(serializedDetails.get());
    assertThat(details.get(new DataWord(key))).isEqualTo(new DataWord(value));
}
Also used : Address(org.aion.base.type.Address) IByteArrayKeyValueDatabase(org.aion.base.db.IByteArrayKeyValueDatabase) IRepositoryCache(org.aion.base.db.IRepositoryCache) AionRepositoryImpl(org.aion.zero.impl.db.AionRepositoryImpl) DataWord(org.aion.mcf.vm.types.DataWord) AionContractDetailsImpl(org.aion.zero.db.AionContractDetailsImpl) Test(org.junit.Test)

Example 25 with DataWord

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

the class AionRepositoryDummy method updateBatch.

public void updateBatch(HashMap<ByteArrayWrapper, AccountState> stateCache, HashMap<ByteArrayWrapper, IContractDetails<DataWord>> detailsCache) {
    for (ByteArrayWrapper hash : stateCache.keySet()) {
        AccountState accountState = stateCache.get(hash);
        IContractDetails<DataWord> contractDetails = detailsCache.get(hash);
        if (accountState.isDeleted()) {
            worldState.remove(hash);
            detailsDB.remove(hash);
            logger.debug("delete: [{}]", Hex.toHexString(hash.getData()));
        } else {
            if (accountState.isDirty() || contractDetails.isDirty()) {
                detailsDB.put(hash, contractDetails);
                accountState.setStateRoot(contractDetails.getStorageHash());
                accountState.setCodeHash(h256(contractDetails.getCode()));
                worldState.put(hash, accountState);
                if (logger.isDebugEnabled()) {
                    logger.debug("update: [{}],nonce: [{}] balance: [{}] \n [{}]", Hex.toHexString(hash.getData()), accountState.getNonce(), accountState.getBalance(), contractDetails.getStorage());
                }
            }
        }
    }
    stateCache.clear();
    detailsCache.clear();
}
Also used : ByteArrayWrapper(org.aion.base.util.ByteArrayWrapper) DataWord(org.aion.mcf.vm.types.DataWord) AccountState(org.aion.mcf.core.AccountState)

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