Skip to content

Commit

Permalink
Merge MutableAccount and EVMAccount (hyperledger#5863)
Browse files Browse the repository at this point in the history
Merge MutableAccount and EVMAccount functionalities by removing
EVMAccount, all calls to getMutable, and the WrappedEVMAccount that was
wrapping non-EVMAccounts in a mutable fashion.  Instead, use a
MutableAccount in all cases an EVMAccount would have been used.  This
also tends to reduce a level of layering in many places.

Signed-off-by: Danno Ferrin <danno.ferrin@swirldslabs.com>
Signed-off-by: garyschulte <garyschulte@gmail.com>
  • Loading branch information
shemnon authored and garyschulte committed Sep 20, 2023
1 parent eb2c430 commit 9ec9fcb
Show file tree
Hide file tree
Showing 51 changed files with 401 additions and 608 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
import org.hyperledger.besu.ethereum.mainnet.feemarket.BaseFeeMarket;
import org.hyperledger.besu.ethereum.mainnet.feemarket.ExcessBlobGasCalculator;
import org.hyperledger.besu.ethereum.mainnet.feemarket.FeeMarket;
import org.hyperledger.besu.evm.account.EvmAccount;
import org.hyperledger.besu.evm.account.MutableAccount;
import org.hyperledger.besu.evm.worldstate.WorldUpdater;
import org.hyperledger.besu.plugin.services.exception.StorageException;
import org.hyperledger.besu.plugin.services.securitymodule.SecurityModuleException;
Expand Down Expand Up @@ -190,10 +190,10 @@ protected BlockCreationResult createBlock(

final List<BlockHeader> ommers = maybeOmmers.orElse(selectOmmers());

if (maybeParentBeaconBlockRoot.isPresent()) {
ParentBeaconBlockRootHelper.storeParentBeaconBlockRoot(
disposableWorldState.updater(), timestamp, maybeParentBeaconBlockRoot.get());
}
maybeParentBeaconBlockRoot.ifPresent(
bytes32 ->
ParentBeaconBlockRootHelper.storeParentBeaconBlockRoot(
disposableWorldState.updater(), timestamp, bytes32));

throwIfStopped();
final TransactionSelectionResults transactionResults =
Expand Down Expand Up @@ -466,9 +466,9 @@ boolean rewardBeneficiary(
.getBlockProcessor()
.getCoinbaseReward(blockReward, header.getNumber(), ommers.size());
final WorldUpdater updater = worldState.updater();
final EvmAccount beneficiary = updater.getOrCreate(miningBeneficiary);
final MutableAccount beneficiary = updater.getOrCreate(miningBeneficiary);

beneficiary.getMutable().incrementBalance(coinbaseReward);
beneficiary.incrementBalance(coinbaseReward);
for (final BlockHeader ommerHeader : ommers) {
if (ommerHeader.getNumber() - header.getNumber() > MAX_GENERATION) {
LOG.trace(
Expand All @@ -479,12 +479,12 @@ boolean rewardBeneficiary(
return false;
}

final EvmAccount ommerCoinbase = updater.getOrCreate(ommerHeader.getCoinbase());
final MutableAccount ommerCoinbase = updater.getOrCreate(ommerHeader.getCoinbase());
final Wei ommerReward =
protocolSpec
.getBlockProcessor()
.getOmmerReward(blockReward, header.getNumber(), ommerHeader.getNumber());
ommerCoinbase.getMutable().incrementBalance(ommerReward);
ommerCoinbase.incrementBalance(ommerReward);
}

updater.commit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class EntriesFromIntegrationTest {
public void shouldCollectStateEntries() {
final MutableWorldState worldState = createInMemoryWorldStateArchive().getMutable();
final WorldUpdater updater = worldState.updater();
MutableAccount account = updater.getOrCreate(Address.fromHexString("0x56")).getMutable();
MutableAccount account = updater.getOrCreate(Address.fromHexString("0x56"));
final Map<Bytes32, AccountStorageEntry> expectedValues = new TreeMap<>();
final int nodeCount = 100_000;
final Random random = new Random(42989428249L);
Expand All @@ -49,19 +49,19 @@ public void shouldCollectStateEntries() {
addExpectedValue(
account,
expectedValues,
UInt256.valueOf(Math.abs(random.nextLong())),
UInt256.valueOf(i * 10 + 1));
UInt256.valueOf(random.nextLong(Long.MAX_VALUE)),
UInt256.valueOf(i * 10 + 1L));
}
updater.commit();

// Add some changes on top that AbstractWorldUpdater.UpdateTrackingAccount will have to merge.
account = worldState.updater().getOrCreate(Address.fromHexString("0x56")).getMutable();
account = worldState.updater().getOrCreate(Address.fromHexString("0x56"));
for (int i = 0; i <= nodeCount; i++) {
addExpectedValue(
account,
expectedValues,
UInt256.valueOf(Math.abs(random.nextLong())),
UInt256.valueOf(i * 10 + 1));
UInt256.valueOf(random.nextLong(Long.MAX_VALUE)),
UInt256.valueOf(i * 10 + 1L));
}

final Map<Bytes32, AccountStorageEntry> values =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private MessageFrame createMessageFrame(final Address address) {
.blockHeader(blockHeader)
.blockchain(blockchain)
.build();
worldStateUpdater.getOrCreate(address).getMutable().setBalance(Wei.of(1));
worldStateUpdater.getOrCreate(address).setBalance(Wei.of(1));
worldStateUpdater.commit();

return benchmarkFrame;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.hyperledger.besu.ethereum.rlp.RLPOutput;
import org.hyperledger.besu.evm.ModificationNotAllowedException;
import org.hyperledger.besu.evm.account.AccountStorageEntry;
import org.hyperledger.besu.evm.account.EvmAccount;
import org.hyperledger.besu.evm.account.MutableAccount;
import org.hyperledger.besu.evm.worldstate.UpdateTrackingAccount;

Expand All @@ -41,9 +40,9 @@
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.units.bigints.UInt256;

public class BonsaiAccount implements MutableAccount, EvmAccount, AccountValue {
public class BonsaiAccount implements MutableAccount, AccountValue {
private final BonsaiWorldView context;
private final boolean mutable;
private boolean mutable;

private final Address address;
private final Hash addressHash;
Expand Down Expand Up @@ -163,7 +162,7 @@ public long getNonce() {
@Override
public void setNonce(final long value) {
if (!mutable) {
throw new UnsupportedOperationException("Account is immutable");
throw new ModificationNotAllowedException();
}
nonce = value;
}
Expand All @@ -176,7 +175,7 @@ public Wei getBalance() {
@Override
public void setBalance(final Wei value) {
if (!mutable) {
throw new UnsupportedOperationException("Account is immutable");
throw new ModificationNotAllowedException();
}
balance = value;
}
Expand All @@ -192,7 +191,7 @@ public Bytes getCode() {
@Override
public void setCode(final Bytes code) {
if (!mutable) {
throw new UnsupportedOperationException("Account is immutable");
throw new ModificationNotAllowedException();
}
this.code = code;
if (code == null || code.isEmpty()) {
Expand Down Expand Up @@ -244,7 +243,7 @@ public void writeTo(final RLPOutput out) {
@Override
public void setStorageValue(final UInt256 key, final UInt256 value) {
if (!mutable) {
throw new UnsupportedOperationException("Account is immutable");
throw new ModificationNotAllowedException();
}
updatedStorage.put(key, value);
}
Expand All @@ -259,27 +258,23 @@ public Map<UInt256, UInt256> getUpdatedStorage() {
return updatedStorage;
}

@Override
public MutableAccount getMutable() throws ModificationNotAllowedException {
if (mutable) {
return this;
} else {
throw new ModificationNotAllowedException();
}
}

@Override
public Hash getStorageRoot() {
return storageRoot;
}

public void setStorageRoot(final Hash storageRoot) {
if (!mutable) {
throw new UnsupportedOperationException("Account is immutable");
throw new ModificationNotAllowedException();
}
this.storageRoot = storageRoot;
}

@Override
public void becomeImmutable() {
mutable = false;
}

@Override
public String toString() {
return "AccountState{"
Expand Down
Loading

0 comments on commit 9ec9fcb

Please sign in to comment.