Search in sources :

Example 11 with MutableAccount

use of org.hyperledger.besu.evm.account.MutableAccount in project besu by hyperledger.

the class DefaultMutableWorldStateTest method replaceAccountBalance.

@Test
public void replaceAccountBalance() {
    final MutableWorldState worldState = createEmpty();
    final WorldUpdater updater = worldState.updater();
    final MutableAccount account = updater.createAccount(ADDRESS).getMutable();
    account.setBalance(Wei.of(100000));
    account.setBalance(Wei.of(200000));
    updater.commit();
    assertThat(worldState.get(ADDRESS).getBalance()).isEqualTo(Wei.of(200000));
    assertThat(worldState.rootHash()).isEqualTo(Hash.fromHexString("0xbfa4e0598cc2b810a8ccc4a2d9a4c575574d05c9c4a7f915e6b8545953a5051e"));
}
Also used : MutableWorldState(org.hyperledger.besu.ethereum.core.MutableWorldState) WorldUpdater(org.hyperledger.besu.evm.worldstate.WorldUpdater) MutableAccount(org.hyperledger.besu.evm.account.MutableAccount) Test(org.junit.Test)

Example 12 with MutableAccount

use of org.hyperledger.besu.evm.account.MutableAccount in project besu by hyperledger.

the class DefaultMutableWorldStateTest method streamAccounts_multipleAccounts.

@Test
public void streamAccounts_multipleAccounts() {
    final Address addr1 = Address.fromHexString("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b");
    final Address addr2 = Address.fromHexString("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0c");
    final MutableWorldState worldState = createEmpty();
    final WorldUpdater updater = worldState.updater();
    // Create an account
    final MutableAccount accountA = updater.createAccount(addr1).getMutable();
    accountA.setBalance(Wei.of(100000));
    // Create another
    final MutableAccount accountB = updater.createAccount(addr2).getMutable();
    accountB.setNonce(1);
    // Commit changes
    updater.commit();
    final boolean accountAIsFirst = accountA.getAddressHash().toUnsignedBigInteger().compareTo(accountB.getAddressHash().toUnsignedBigInteger()) < 0;
    final Hash startHash = accountAIsFirst ? accountA.getAddressHash() : accountB.getAddressHash();
    // Get first account
    final List<StreamableAccount> firstAccount = worldState.streamAccounts(startHash, 1).collect(Collectors.toList());
    assertThat(firstAccount.size()).isEqualTo(1L);
    assertThat(firstAccount.get(0).getAddress()).hasValue(accountAIsFirst ? accountA.getAddress() : accountB.getAddress());
    // Get both accounts
    final List<StreamableAccount> allAccounts = worldState.streamAccounts(Bytes32.ZERO, 2).collect(Collectors.toList());
    assertThat(allAccounts.size()).isEqualTo(2L);
    assertThat(allAccounts.get(0).getAddress()).hasValue(accountAIsFirst ? accountA.getAddress() : accountB.getAddress());
    assertThat(allAccounts.get(1).getAddress()).hasValue(accountAIsFirst ? accountB.getAddress() : accountA.getAddress());
    // Get second account
    final Bytes32 startHashForSecondAccount = UInt256.fromBytes(startHash).add(1L);
    final List<StreamableAccount> secondAccount = worldState.streamAccounts(startHashForSecondAccount, 100).collect(Collectors.toList());
    assertThat(secondAccount.size()).isEqualTo(1L);
    assertThat(secondAccount.get(0).getAddress()).hasValue(accountAIsFirst ? accountB.getAddress() : accountA.getAddress());
}
Also used : StreamableAccount(org.hyperledger.besu.evm.worldstate.WorldState.StreamableAccount) Address(org.hyperledger.besu.datatypes.Address) MutableWorldState(org.hyperledger.besu.ethereum.core.MutableWorldState) WorldUpdater(org.hyperledger.besu.evm.worldstate.WorldUpdater) MutableAccount(org.hyperledger.besu.evm.account.MutableAccount) Hash(org.hyperledger.besu.datatypes.Hash) Bytes32(org.apache.tuweni.bytes.Bytes32) Test(org.junit.Test)

Example 13 with MutableAccount

use of org.hyperledger.besu.evm.account.MutableAccount in project besu by hyperledger.

the class DefaultMutableWorldStateTest method clearStorageThenEditAfterPersisting.

@Test
public void clearStorageThenEditAfterPersisting() {
    final UInt256 storageKey = UInt256.ONE;
    final UInt256 originalStorageValue = UInt256.valueOf(2L);
    final UInt256 newStorageValue = UInt256.valueOf(3L);
    // Create a world state with one account
    final MutableWorldState worldState = createEmpty();
    final WorldUpdater updater = worldState.updater();
    MutableAccount account = updater.createAccount(ADDRESS).getMutable();
    account.setBalance(Wei.of(100000));
    account.setStorageValue(storageKey, originalStorageValue);
    assertThat(account.getStorageValue(storageKey)).isEqualTo(originalStorageValue);
    updater.commit();
    worldState.persist(null);
    // Clear storage then edit
    account = updater.getAccount(ADDRESS).getMutable();
    assertThat(account).isNotNull();
    assertThat(account.getStorageValue(storageKey)).isEqualTo(originalStorageValue);
    assertThat(updater.get(ADDRESS).getStorageValue(storageKey)).isEqualTo(originalStorageValue);
    account.clearStorage();
    account.setStorageValue(storageKey, newStorageValue);
    assertThat(account.getStorageValue(storageKey)).isEqualTo(newStorageValue);
    assertThat(worldState.get(ADDRESS).getStorageValue(storageKey)).isEqualTo(originalStorageValue);
    // Check storage is cleared after committing
    updater.commit();
    assertThat(updater.getAccount(ADDRESS).getStorageValue(storageKey)).isEqualTo(newStorageValue);
    assertThat(updater.get(ADDRESS).getStorageValue(storageKey)).isEqualTo(newStorageValue);
    assertThat(worldState.get(ADDRESS).getStorageValue(storageKey)).isEqualTo(newStorageValue);
    // And after persisting
    assertThat(updater.getAccount(ADDRESS).getStorageValue(storageKey)).isEqualTo(newStorageValue);
    assertThat(updater.get(ADDRESS).getStorageValue(storageKey)).isEqualTo(newStorageValue);
    assertThat(worldState.get(ADDRESS).getStorageValue(storageKey)).isEqualTo(newStorageValue);
}
Also used : MutableWorldState(org.hyperledger.besu.ethereum.core.MutableWorldState) WorldUpdater(org.hyperledger.besu.evm.worldstate.WorldUpdater) MutableAccount(org.hyperledger.besu.evm.account.MutableAccount) UInt256(org.apache.tuweni.units.bigints.UInt256) Test(org.junit.Test)

Example 14 with MutableAccount

use of org.hyperledger.besu.evm.account.MutableAccount in project besu by hyperledger.

the class DefaultMutableWorldStateTest method revert.

@Test
public void revert() {
    final MutableWorldState worldState = createEmpty();
    final WorldUpdater updater1 = worldState.updater();
    final MutableAccount account1 = updater1.createAccount(ADDRESS).getMutable();
    account1.setBalance(Wei.of(200000));
    updater1.commit();
    final WorldUpdater updater2 = worldState.updater();
    final MutableAccount account2 = updater2.getAccount(ADDRESS).getMutable();
    account2.setBalance(Wei.of(300000));
    assertThat(updater2.get(ADDRESS).getBalance()).isEqualTo(Wei.of(300000));
    updater2.revert();
    assertThat(updater2.get(ADDRESS).getBalance()).isEqualTo(Wei.of(200000));
    updater2.commit();
    assertThat(worldState.get(ADDRESS).getBalance()).isEqualTo(Wei.of(200000));
    assertThat(worldState.rootHash()).isEqualTo(Hash.fromHexString("0xbfa4e0598cc2b810a8ccc4a2d9a4c575574d05c9c4a7f915e6b8545953a5051e"));
}
Also used : MutableWorldState(org.hyperledger.besu.ethereum.core.MutableWorldState) WorldUpdater(org.hyperledger.besu.evm.worldstate.WorldUpdater) MutableAccount(org.hyperledger.besu.evm.account.MutableAccount) Test(org.junit.Test)

Example 15 with MutableAccount

use of org.hyperledger.besu.evm.account.MutableAccount in project besu by hyperledger.

the class DefaultMutableWorldStateTest method setStorageValue_NonzeroValue.

@Test
public void setStorageValue_NonzeroValue() {
    final MutableWorldState worldState = createEmpty();
    final WorldUpdater updater = worldState.updater();
    final MutableAccount account = updater.createAccount(ADDRESS).getMutable();
    account.setBalance(Wei.of(100000));
    account.setStorageValue(UInt256.ONE, UInt256.valueOf(2));
    updater.commit();
    assertThat(worldState.get(ADDRESS).getStorageValue(UInt256.ONE)).isEqualTo(UInt256.valueOf(2));
    assertThat(worldState.rootHash()).isEqualTo(Hash.fromHexString("0xd31ce0bf3bf8790083a8ebde418244fda3b1cca952d7119ed244f86d03044656"));
}
Also used : MutableWorldState(org.hyperledger.besu.ethereum.core.MutableWorldState) WorldUpdater(org.hyperledger.besu.evm.worldstate.WorldUpdater) MutableAccount(org.hyperledger.besu.evm.account.MutableAccount) Test(org.junit.Test)

Aggregations

MutableAccount (org.hyperledger.besu.evm.account.MutableAccount)49 WorldUpdater (org.hyperledger.besu.evm.worldstate.WorldUpdater)33 Test (org.junit.Test)23 MutableWorldState (org.hyperledger.besu.ethereum.core.MutableWorldState)19 Address (org.hyperledger.besu.datatypes.Address)14 Wei (org.hyperledger.besu.datatypes.Wei)14 UInt256 (org.apache.tuweni.units.bigints.UInt256)10 Bytes (org.apache.tuweni.bytes.Bytes)9 MessageFrame (org.hyperledger.besu.evm.frame.MessageFrame)7 Bytes32 (org.apache.tuweni.bytes.Bytes32)5 Hash (org.hyperledger.besu.datatypes.Hash)5 ProcessableBlockHeader (org.hyperledger.besu.ethereum.core.ProcessableBlockHeader)5 ArrayDeque (java.util.ArrayDeque)4 ArrayList (java.util.ArrayList)4 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)4 Code (org.hyperledger.besu.evm.Code)4 Gas (org.hyperledger.besu.evm.Gas)4 Account (org.hyperledger.besu.evm.account.Account)4 Map (java.util.Map)3 TreeMap (java.util.TreeMap)3