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"));
}
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());
}
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);
}
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"));
}
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"));
}
Aggregations