Skip to content

Commit

Permalink
Fix whitelistpositivecase test in LeakyContractTestsSuite (#9955)
Browse files Browse the repository at this point in the history
Signed-off-by: Valentin Valkanov <valentin.valkanov@limechain.tech>
  • Loading branch information
MrValioBg committed Nov 28, 2023
1 parent df69e17 commit 2613f05
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
@ConfigData("contracts")
public record ContractsConfig(
@ConfigProperty(defaultValue = "true") @NetworkProperty boolean itemizeStorageFees,
// @ConfigProperty(defaultValue = "1062787,1461860") Set<Address> permittedDelegateCallers,
@ConfigProperty(defaultValue = "1062787,1461860") Set<Long> permittedDelegateCallers,
@ConfigProperty(defaultValue = "31536000") @NetworkProperty long referenceSlotLifetime,
@ConfigProperty(defaultValue = "100") @NetworkProperty int freeStorageTierLimit,
@ConfigProperty(defaultValue = "0til100M,2000til450M") @NetworkProperty String storageSlotPriceTiers,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.hedera.node.app.service.contract.impl.exec.processors.CustomMessageCallProcessor;
import com.hedera.node.app.service.contract.impl.infra.StorageAccessTracker;
import com.hedera.node.app.service.contract.impl.state.ProxyWorldUpdater;
import com.hedera.node.app.service.contract.impl.utils.ConversionUtils;
import com.hedera.node.config.data.ContractsConfig;
import com.swirlds.config.api.Configuration;
import edu.umd.cs.findbugs.annotations.NonNull;
Expand Down Expand Up @@ -136,11 +137,16 @@ public static boolean unqualifiedDelegateDetected(final MessageFrame frame) {
if (!isDelegateCall(frame)) {
return false;
}
final Address recipient = frame.getRecipientAddress();

final var recipient = frame.getRecipientAddress();
// but we accept delegates if the token redirect contract calls us,
// so if they are not a token, then we are a delegate and we are done.
if (isToken(frame, recipient)) {
final var permittedDelegateCallers = contractsConfigOf(frame).permittedDelegateCallers();

// Evaluate whether the recipient is either a token or on the permitted callers list.
// This determines if we should treat this as a delegate call.
// We accept delegates if the token redirect contract calls us.
if (isToken(frame, recipient)
|| (ConversionUtils.isLongZero(recipient)
&& permittedDelegateCallers.contains(ConversionUtils.numberOfLongZero(recipient)))) {
// make sure we have a parent calling context
final var stack = frame.getMessageFrameStack();
final var frames = stack.iterator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import org.apache.tuweni.units.bigints.UInt256;
import org.hyperledger.besu.datatypes.Address;
import org.hyperledger.besu.datatypes.Wei;
Expand Down Expand Up @@ -131,6 +132,9 @@ public class TestHelpers {
.withValue("contracts.allowAutoAssociations", true)
.getOrCreateConfig();

public static final Configuration PERMITTED_CALLERS_CONFIG = HederaTestConfigBuilder.create()
.withValue("contracts.permittedContractCallers", Set.of(1062787L))
.getOrCreateConfig();
public static final Configuration DEV_CHAIN_ID_CONFIG =
HederaTestConfigBuilder.create().withValue("contracts.chainId", 298).getOrCreateConfig();
public static final LedgerConfig AUTO_ASSOCIATING_LEDGER_CONFIG =
Expand Down Expand Up @@ -392,7 +396,8 @@ public class TestHelpers {
.contractNum(numberOfLongZero(NON_SYSTEM_LONG_ZERO_ADDRESS))
.build();
public static final Address EIP_1014_ADDRESS = Address.fromHexString("0x89abcdef89abcdef89abcdef89abcdef89abcdef");

public static final Address PERMITTED_ADDRESS_CALLER =
Address.wrap((org.apache.tuweni.bytes.Bytes.wrap(asEvmAddress(1062787L))));
public static final Account OPERATOR =
Account.newBuilder().accountId(B_NEW_ACCOUNT_ID).build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import static com.hedera.node.app.service.contract.impl.test.TestHelpers.DEFAULT_CONFIG;
import static com.hedera.node.app.service.contract.impl.test.TestHelpers.EIP_1014_ADDRESS;
import static com.hedera.node.app.service.contract.impl.test.TestHelpers.NON_SYSTEM_LONG_ZERO_ADDRESS;
import static com.hedera.node.app.service.contract.impl.test.TestHelpers.PERMITTED_ADDRESS_CALLER;
import static com.hedera.node.app.service.contract.impl.test.TestHelpers.PERMITTED_CALLERS_CONFIG;
import static com.hedera.node.app.service.evm.store.contracts.HederaEvmWorldStateTokenAccount.TOKEN_PROXY_ACCOUNT_NONCE;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
Expand All @@ -44,7 +47,9 @@
import java.util.Deque;
import java.util.HashSet;
import java.util.Set;
import org.hyperledger.besu.evm.account.MutableAccount;
import org.hyperledger.besu.evm.frame.MessageFrame;
import org.hyperledger.besu.evm.worldstate.WorldUpdater;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -68,6 +73,12 @@ class FrameUtilsTest {
@Mock
private MessageFrame initialFrame;

@Mock
private MutableAccount account;

@Mock
private WorldUpdater worldUpdater;

private final Deque<MessageFrame> stack = new ArrayDeque<>();

@Test
Expand Down Expand Up @@ -103,6 +114,73 @@ void childOfParentExecutingItsOwnCodeDoesNotAcquireSenderAuthorizationViaDelegat
assertFalse(FrameUtils.acquiredSenderAuthorizationViaDelegateCall(frame));
}

@Test
void unqualifiedDelegateDetectedValidationPass() {
// given
stack.push(initialFrame);
stack.push(frame);
given(frame.getWorldUpdater()).willReturn(worldUpdater);
given(frame.getMessageFrameStack()).willReturn(stack);

given(frame.getRecipientAddress()).willReturn(EIP_1014_ADDRESS);
given(frame.getContractAddress()).willReturn(NON_SYSTEM_LONG_ZERO_ADDRESS);
given(initialFrame.getRecipientAddress()).willReturn(EIP_1014_ADDRESS);
given(initialFrame.getContractAddress()).willReturn(EIP_1014_ADDRESS);

given(worldUpdater.get(EIP_1014_ADDRESS)).willReturn(account);
given(account.getNonce()).willReturn(TOKEN_PROXY_ACCOUNT_NONCE);
given(initialFrame.getContextVariable(CONFIG_CONTEXT_VARIABLE)).willReturn(DEFAULT_CONFIG);

// when
final var isQualifiedForDelegate = !FrameUtils.unqualifiedDelegateDetected(frame);

// then
assertTrue(isQualifiedForDelegate);
}

@Test
void unqualifiedDelegateDetectedValidationFailTokenNull() {
// given
givenNonInitialFrame();
given(frame.getMessageFrameStack()).willReturn(stack);
given(frame.getWorldUpdater()).willReturn(worldUpdater);

given(frame.getRecipientAddress()).willReturn(EIP_1014_ADDRESS);
given(frame.getContractAddress()).willReturn(NON_SYSTEM_LONG_ZERO_ADDRESS);

given(worldUpdater.get(EIP_1014_ADDRESS)).willReturn(null);
given(initialFrame.getContextVariable(CONFIG_CONTEXT_VARIABLE)).willReturn(DEFAULT_CONFIG);

// when
final var isQualifiedForDelegate = !FrameUtils.unqualifiedDelegateDetected(frame);

// then
assertFalse(isQualifiedForDelegate);
}

@Test
void unqualifiedDelegateDetectedValidationPassWithPermittedCaller() {
// given
stack.push(initialFrame);
stack.push(frame);
given(frame.getMessageFrameStack()).willReturn(stack);
given(frame.getWorldUpdater()).willReturn(worldUpdater);

given(frame.getRecipientAddress()).willReturn(PERMITTED_ADDRESS_CALLER);
given(frame.getContractAddress()).willReturn(NON_SYSTEM_LONG_ZERO_ADDRESS);
given(initialFrame.getRecipientAddress()).willReturn(PERMITTED_ADDRESS_CALLER);
given(initialFrame.getContractAddress()).willReturn(PERMITTED_ADDRESS_CALLER);

given(worldUpdater.get(PERMITTED_ADDRESS_CALLER)).willReturn(null);
given(initialFrame.getContextVariable(CONFIG_CONTEXT_VARIABLE)).willReturn(PERMITTED_CALLERS_CONFIG);

// when
final var isQualifiedForDelegate = !FrameUtils.unqualifiedDelegateDetected(frame);

// then
assertTrue(isQualifiedForDelegate);
}

@Test
void childOfParentExecutingDelegateCodeDoesAcquireSenderAuthorizationViaDelegateCall() {
given(initialFrame.getRecipientAddress()).willReturn(EIP_1014_ADDRESS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2036,6 +2036,7 @@ private HapiSpec erc20TransferFromDoesNotWorkIfFlagIsDisabled() {
overriding(HEDERA_ALLOWANCES_IS_ENABLED, "true"));
}

@HapiTest
private HapiSpec whitelistPositiveCase() {
final AtomicLong whitelistedCalleeMirrorNum = new AtomicLong();
final AtomicReference<TokenID> tokenID = new AtomicReference<>();
Expand Down

0 comments on commit 2613f05

Please sign in to comment.