Skip to content

Commit

Permalink
Merge branch 'main' into withdrawals_add_to_block
Browse files Browse the repository at this point in the history
  • Loading branch information
jframe authored Jan 17, 2023
2 parents 92176c9 + df865d9 commit 84a97b4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ static ProtocolSpecBuilder shanghaiDefinition(
TransactionType.EIP1559),
quorumCompatibilityMode,
SHANGHAI_INIT_CODE_SIZE_LIMIT))
.withdrawalsProcessor(new WithdrawalsProcessor.DefaultWithdrawalsProcessor())
.withdrawalsProcessor(new WithdrawalsProcessor())
.withdrawalsValidator(new WithdrawalsValidator.AllowedWithdrawals())
.name("Shanghai");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,14 @@

import java.util.List;

public interface WithdrawalsProcessor {
public class WithdrawalsProcessor {

void processWithdrawals(final List<Withdrawal> withdrawals, final WorldUpdater worldUpdater);

class NoOpWithdrawalsProcessor implements WithdrawalsProcessor {

@Override
public void processWithdrawals(
final List<Withdrawal> withdrawals, final WorldUpdater worldUpdater) {}
}

class DefaultWithdrawalsProcessor implements WithdrawalsProcessor {

@Override
public void processWithdrawals(
final List<Withdrawal> withdrawals, final WorldUpdater worldUpdater) {
for (final Withdrawal withdrawal : withdrawals) {
final EvmAccount account = worldUpdater.getOrCreate(withdrawal.getAddress());
account.getMutable().incrementBalance(withdrawal.getAmount());
}
worldUpdater.commit();
public void processWithdrawals(
final List<Withdrawal> withdrawals, final WorldUpdater worldUpdater) {
for (final Withdrawal withdrawal : withdrawals) {
final EvmAccount account = worldUpdater.getOrCreate(withdrawal.getAddress());
account.getMutable().incrementBalance(withdrawal.getAmount());
}
worldUpdater.commit();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,46 +33,15 @@

class WithdrawalsProcessorTest {

private final WithdrawalsProcessor.DefaultWithdrawalsProcessor defaultWithdrawalsProcessor =
new WithdrawalsProcessor.DefaultWithdrawalsProcessor();

@Test
void noopProcessor_shouldNotProcessWithdrawals() {
final MutableWorldState worldState =
createWorldStateWithAccounts(List.of(entry("0x1", 1), entry("0x2", 2), entry("0x3", 3)));
final MutableWorldState originalState = worldState.copy();
final WorldUpdater updater = worldState.updater();

final List<Withdrawal> withdrawals =
List.of(
new Withdrawal(
UInt64.valueOf(100),
UInt64.valueOf(1000),
Address.fromHexString("0x1"),
Wei.of(100)),
new Withdrawal(
UInt64.valueOf(200),
UInt64.valueOf(2000),
Address.fromHexString("0x2"),
Wei.of(200)));
final WithdrawalsProcessor noopWithdrawalsProcessor =
new WithdrawalsProcessor.NoOpWithdrawalsProcessor();
noopWithdrawalsProcessor.processWithdrawals(withdrawals, updater);

assertThat(worldState.get(Address.fromHexString("0x1")).getBalance()).isEqualTo(Wei.of(1));
assertThat(worldState.get(Address.fromHexString("0x2")).getBalance()).isEqualTo(Wei.of(2));
assertThat(worldState.get(Address.fromHexString("0x3")).getBalance()).isEqualTo(Wei.of(3));
assertThat(originalState).isEqualTo(worldState);
}

@Test
void defaultProcessor_shouldProcessEmptyWithdrawalsWithoutChangingWorldState() {
final MutableWorldState worldState =
createWorldStateWithAccounts(List.of(entry("0x1", 1), entry("0x2", 2), entry("0x3", 3)));
final MutableWorldState originalState = worldState.copy();
final WorldUpdater updater = worldState.updater();

defaultWithdrawalsProcessor.processWithdrawals(Collections.emptyList(), updater);
final WithdrawalsProcessor withdrawalsProcessor = new WithdrawalsProcessor();
withdrawalsProcessor.processWithdrawals(Collections.emptyList(), updater);

assertThat(worldState.get(Address.fromHexString("0x1")).getBalance()).isEqualTo(Wei.of(1));
assertThat(worldState.get(Address.fromHexString("0x2")).getBalance()).isEqualTo(Wei.of(2));
Expand All @@ -98,7 +67,8 @@ void defaultProcessor_shouldProcessWithdrawalsUpdatingExistingAccountsBalance()
UInt64.valueOf(2000),
Address.fromHexString("0x2"),
Wei.of(200)));
defaultWithdrawalsProcessor.processWithdrawals(withdrawals, updater);
final WithdrawalsProcessor withdrawalsProcessor = new WithdrawalsProcessor();
withdrawalsProcessor.processWithdrawals(withdrawals, updater);

assertThat(worldState.get(Address.fromHexString("0x1")).getBalance()).isEqualTo(Wei.of(101));
assertThat(worldState.get(Address.fromHexString("0x2")).getBalance()).isEqualTo(Wei.of(202));
Expand All @@ -122,7 +92,8 @@ void defaultProcessor_shouldProcessWithdrawalsUpdatingEmptyAccountsBalance() {
UInt64.valueOf(2000),
Address.fromHexString("0x2"),
Wei.of(200)));
defaultWithdrawalsProcessor.processWithdrawals(withdrawals, updater);
final WithdrawalsProcessor withdrawalsProcessor = new WithdrawalsProcessor();
withdrawalsProcessor.processWithdrawals(withdrawals, updater);

assertThat(worldState.get(Address.fromHexString("0x1")).getBalance()).isEqualTo(Wei.of(100));
assertThat(worldState.get(Address.fromHexString("0x2")).getBalance()).isEqualTo(Wei.of(200));
Expand Down

0 comments on commit 84a97b4

Please sign in to comment.