Search in sources :

Example 26 with ByteArrayWrapper

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

the class ExternalStateForFvm method getStorageValue.

/**
 * Returns the value in the key-value pairing associated with the given address and key, or
 * {@code null} if no such pairing exists.
 *
 * @param address The account address.
 * @param key The key.
 * @return the value.
 */
@Override
public FvmDataWord getStorageValue(AionAddress address, FvmDataWord key) {
    ByteArrayWrapper storageKey = ByteArrayWrapper.wrap(key.copyOfData());
    ByteArrayWrapper value = this.repository.getStorageValue(address, storageKey);
    if (value != null && (value.isZero() || value.isEmpty())) {
        // used to ensure FVM correctness
        throw new IllegalStateException("A zero or empty value was retrieved from storage. Storing zeros is not allowed by the FVM. An incorrect put was previously performed instead of an explicit call to the delete method.");
    }
    return (value == null) ? FvmDataWord.fromBytes(new byte[FvmDataWord.SIZE]) : FvmDataWord.fromBytes(value.toBytes());
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper)

Example 27 with ByteArrayWrapper

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

the class ExternalStateForFvm method removeStorage.

/**
 * Removes the provided key from the storage associated with the given address, if that key
 * exists.
 *
 * @param address The account address.
 * @param key The key.
 */
@Override
public void removeStorage(AionAddress address, FvmDataWord key) {
    ByteArrayWrapper storageKey = ByteArrayWrapper.wrap(key.copyOfData());
    this.repository.removeStorageRow(address, storageKey);
    setVmType(address);
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper)

Example 28 with ByteArrayWrapper

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

the class ExternalStateForFvm method addStorageValue.

/**
 * Adds the provided key-value pairing to the world state, associating it only with the given
 * address.
 *
 * <p>If the given address already has a key-value pairing whose key is the same as the given
 * key, then the given value will overwrite whatever value is currently paired with that key.
 *
 * @param address The account address.
 * @param key The key.
 * @param value The value.
 */
@Override
public void addStorageValue(AionAddress address, FvmDataWord key, FvmDataWord value) {
    byte[] valueBytes = value.copyOfData();
    if (valueBytes == null || valueBytes.length == 0) {
        // used to ensure FVM correctness
        throw new IllegalArgumentException("Put with null, empty or zero byte array values is not allowed for the FVM. For deletions, make explicit calls to the delete method.");
    }
    ByteArrayWrapper storageKey = ByteArrayWrapper.wrap(key.copyOfData());
    ByteArrayWrapper storageValue = alignValueToWordSizeForPut(valueBytes);
    if (storageValue.isZero()) {
        // used to ensure FVM correctness
        throw new IllegalArgumentException("Put with null, empty or zero byte array values is not allowed for the FVM. For deletions, make explicit calls to the delete method.");
    }
    this.repository.addStorageRow(address, storageKey, storageValue);
    setVmType(address);
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper)

Example 29 with ByteArrayWrapper

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

the class KeystoreTest method testAccountBackup.

@Test
public void testAccountBackup() {
    String password = randomPassword();
    ECKey key = ECKeyFac.inst().create();
    assertNotNull(key);
    String addr = Keystore.create(password, key);
    assertEquals(addr.substring(2), ByteUtil.toHexString(key.getAddress()));
    Map<AionAddress, String> arg = new HashMap<>();
    arg.put(AddressUtils.wrapAddress(addr), password);
    Map<AionAddress, ByteArrayWrapper> export = Keystore.backupAccount(arg);
    assertNotNull(export);
    File f = Keystore.getAccountFile(addr.substring(2), password);
    assertNotNull(f);
    assertTrue(export.containsKey(AddressUtils.wrapAddress(addr)));
    try {
        assertTrue(export.containsValue(ByteArrayWrapper.wrap(Files.readAllBytes(f.toPath()))));
    } catch (IOException e) {
        e.printStackTrace();
    }
    filesToRemove.add(addr);
}
Also used : AionAddress(org.aion.types.AionAddress) ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) HashMap(java.util.HashMap) ECKey(org.aion.crypto.ECKey) IOException(java.io.IOException) File(java.io.File) Test(org.junit.Test)

Example 30 with ByteArrayWrapper

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

the class KeystoreTest method testAccountExport.

@Test
public void testAccountExport() {
    String password = randomPassword();
    ECKey key = ECKeyFac.inst().create();
    assertNotNull(key);
    String addr = Keystore.create(password, key);
    assertEquals(addr.substring(2), ByteUtil.toHexString(key.getAddress()));
    Map<AionAddress, String> arg = new HashMap<>();
    arg.put(AddressUtils.wrapAddress(addr), password);
    Map<AionAddress, ByteArrayWrapper> export = Keystore.exportAccount(arg);
    assertTrue(export.containsKey(AddressUtils.wrapAddress(addr)));
    assertTrue(export.containsValue(ByteArrayWrapper.wrap(key.getPrivKeyBytes())));
    filesToRemove.add(addr);
}
Also used : AionAddress(org.aion.types.AionAddress) ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) HashMap(java.util.HashMap) ECKey(org.aion.crypto.ECKey) 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