-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor operations to eliminate duplicate code between EVM versions (#…
…7000) Signed-off-by: Michael Tinker <[email protected]>
- Loading branch information
1 parent
6c52a71
commit d75dd6e
Showing
98 changed files
with
6,734 additions
and
197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...pi/src/main/java/com/hedera/node/app/spi/meta/bni/ActiveContractVerificationStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright (C) 2023 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.hedera.node.app.spi.meta.bni; | ||
|
||
import com.hedera.hapi.node.base.ContractID; | ||
import com.hedera.hapi.node.base.Key; | ||
import com.hedera.pbj.runtime.io.buffer.Bytes; | ||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import java.util.Objects; | ||
|
||
/** | ||
* A {@link VerificationStrategy} that verifies signatures from a single active contract. This is the | ||
* verification strategy used within the EVM to check receiver signature requirements. | ||
*/ | ||
public class ActiveContractVerificationStrategy implements VerificationStrategy { | ||
private final long activeNumber; | ||
private final Bytes activeAddress; | ||
private final boolean requiresDelegatePermission; | ||
|
||
public ActiveContractVerificationStrategy( | ||
final long activeNumber, @NonNull final Bytes activeAddress, final boolean requiresDelegatePermission) { | ||
this.activeNumber = activeNumber; | ||
this.activeAddress = Objects.requireNonNull(activeAddress); | ||
this.requiresDelegatePermission = requiresDelegatePermission; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public Decision maybeVerifySignature(@NonNull final Key key, @NonNull final KeyRole keyRole) { | ||
final var keyKind = key.key().kind(); | ||
if (keyKind == Key.KeyOneOfType.CONTRACT_ID) { | ||
if (requiresDelegatePermission) { | ||
return Decision.INVALID; | ||
} else { | ||
return decisionFor(key.contractIDOrThrow()); | ||
} | ||
} else if (keyKind == Key.KeyOneOfType.DELEGATABLE_CONTRACT_ID) { | ||
return decisionFor(key.delegatableContractIdOrThrow()); | ||
} else { | ||
return Decision.DELEGATE_TO_CRYPTOGRAPHIC_VERIFICATION; | ||
} | ||
} | ||
|
||
public long getActiveNumber() { | ||
return activeNumber; | ||
} | ||
|
||
public Bytes getActiveAddress() { | ||
return activeAddress; | ||
} | ||
|
||
public boolean requiresDelegatePermission() { | ||
return requiresDelegatePermission; | ||
} | ||
|
||
private Decision decisionFor(@NonNull final ContractID authorizedId) { | ||
if (authorizedId.hasContractNum() && authorizedId.contractNumOrThrow() == activeNumber) { | ||
return Decision.VALID; | ||
} else if (authorizedId.hasEvmAddress() && activeAddress.equals(authorizedId.evmAddress())) { | ||
return Decision.VALID; | ||
} else { | ||
return Decision.INVALID; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
...rc/test/java/com/hedera/node/app/spi/meta/bni/ActiveContractVerificationStrategyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/* | ||
* Copyright (C) 2023 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.hedera.node.app.spi.meta.bni; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import com.hedera.hapi.node.base.ContractID; | ||
import com.hedera.hapi.node.base.Key; | ||
import com.hedera.hapi.node.token.CryptoTransferTransactionBody; | ||
import com.hedera.pbj.runtime.io.buffer.Bytes; | ||
import java.util.List; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class ActiveContractVerificationStrategyTest { | ||
private static final long ACTIVE_NUMBER = 1234L; | ||
private static final long SOME_OTHER_NUMBER = 2345L; | ||
private static final Bytes ACTIVE_ADDRESS = Bytes.fromHex("1234"); | ||
private static final Bytes OTHER_ADDRESS = Bytes.fromHex("abcd"); | ||
|
||
private static final Key ACTIVE_ID_KEY = Key.newBuilder() | ||
.contractID(ContractID.newBuilder().contractNum(ACTIVE_NUMBER)) | ||
.build(); | ||
private static final Key DELEGATABLE_ACTIVE_ID_KEY = Key.newBuilder() | ||
.delegatableContractId(ContractID.newBuilder().contractNum(ACTIVE_NUMBER)) | ||
.build(); | ||
private static final Key INACTIVE_ID_KEY = Key.newBuilder() | ||
.contractID(ContractID.newBuilder().contractNum(SOME_OTHER_NUMBER)) | ||
.build(); | ||
private static final Key DELEGATABLE_INACTIVE_ID_KEY = Key.newBuilder() | ||
.delegatableContractId(ContractID.newBuilder().contractNum(SOME_OTHER_NUMBER)) | ||
.build(); | ||
private static final Key ACTIVE_ADDRESS_KEY = Key.newBuilder() | ||
.contractID(ContractID.newBuilder().evmAddress(ACTIVE_ADDRESS)) | ||
.build(); | ||
private static final Key DELEGATABLE_ACTIVE_ADDRESS_KEY = Key.newBuilder() | ||
.delegatableContractId(ContractID.newBuilder().evmAddress(ACTIVE_ADDRESS)) | ||
.build(); | ||
private static final Key INACTIVE_ADDRESS_KEY = Key.newBuilder() | ||
.contractID(ContractID.newBuilder().evmAddress(OTHER_ADDRESS)) | ||
.build(); | ||
private static final Key DELEGATABLE_INACTIVE_ADDRESS_KEY = Key.newBuilder() | ||
.delegatableContractId(ContractID.newBuilder().evmAddress(OTHER_ADDRESS)) | ||
.build(); | ||
private static final Key CRYPTO_KEY = Key.newBuilder() | ||
.ed25519(Bytes.fromHex("1234567812345678123456781234567812345678123456781234567812345678")) | ||
.build(); | ||
|
||
@Test | ||
void validatesKeysAsExpectedWhenDelegatePermissionNotRequired() { | ||
final var subject = new ActiveContractVerificationStrategy(ACTIVE_NUMBER, ACTIVE_ADDRESS, false); | ||
|
||
assertEquals( | ||
VerificationStrategy.Decision.VALID, | ||
subject.maybeVerifySignature(ACTIVE_ID_KEY, VerificationStrategy.KeyRole.OTHER)); | ||
assertEquals( | ||
VerificationStrategy.Decision.VALID, | ||
subject.maybeVerifySignature(ACTIVE_ADDRESS_KEY, VerificationStrategy.KeyRole.OTHER)); | ||
assertEquals( | ||
VerificationStrategy.Decision.INVALID, | ||
subject.maybeVerifySignature(INACTIVE_ID_KEY, VerificationStrategy.KeyRole.OTHER)); | ||
assertEquals( | ||
VerificationStrategy.Decision.INVALID, | ||
subject.maybeVerifySignature(INACTIVE_ADDRESS_KEY, VerificationStrategy.KeyRole.OTHER)); | ||
assertEquals( | ||
VerificationStrategy.Decision.VALID, | ||
subject.maybeVerifySignature(DELEGATABLE_ACTIVE_ID_KEY, VerificationStrategy.KeyRole.OTHER)); | ||
assertEquals( | ||
VerificationStrategy.Decision.VALID, | ||
subject.maybeVerifySignature(DELEGATABLE_ACTIVE_ADDRESS_KEY, VerificationStrategy.KeyRole.OTHER)); | ||
assertEquals( | ||
VerificationStrategy.Decision.INVALID, | ||
subject.maybeVerifySignature(DELEGATABLE_INACTIVE_ID_KEY, VerificationStrategy.KeyRole.OTHER)); | ||
assertEquals( | ||
VerificationStrategy.Decision.INVALID, | ||
subject.maybeVerifySignature(DELEGATABLE_INACTIVE_ADDRESS_KEY, VerificationStrategy.KeyRole.OTHER)); | ||
assertEquals( | ||
VerificationStrategy.Decision.DELEGATE_TO_CRYPTOGRAPHIC_VERIFICATION, | ||
subject.maybeVerifySignature(CRYPTO_KEY, VerificationStrategy.KeyRole.OTHER)); | ||
} | ||
|
||
@Test | ||
void validatesKeysAsExpectedWhenDelegatePermissionRequired() { | ||
final var subject = new ActiveContractVerificationStrategy(ACTIVE_NUMBER, ACTIVE_ADDRESS, true); | ||
|
||
assertEquals( | ||
VerificationStrategy.Decision.INVALID, | ||
subject.maybeVerifySignature(ACTIVE_ID_KEY, VerificationStrategy.KeyRole.OTHER)); | ||
assertEquals( | ||
VerificationStrategy.Decision.INVALID, | ||
subject.maybeVerifySignature(ACTIVE_ADDRESS_KEY, VerificationStrategy.KeyRole.OTHER)); | ||
assertEquals( | ||
VerificationStrategy.Decision.INVALID, | ||
subject.maybeVerifySignature(INACTIVE_ID_KEY, VerificationStrategy.KeyRole.OTHER)); | ||
assertEquals( | ||
VerificationStrategy.Decision.INVALID, | ||
subject.maybeVerifySignature(INACTIVE_ADDRESS_KEY, VerificationStrategy.KeyRole.OTHER)); | ||
assertEquals( | ||
VerificationStrategy.Decision.VALID, | ||
subject.maybeVerifySignature(DELEGATABLE_ACTIVE_ID_KEY, VerificationStrategy.KeyRole.OTHER)); | ||
assertEquals( | ||
VerificationStrategy.Decision.VALID, | ||
subject.maybeVerifySignature(DELEGATABLE_ACTIVE_ADDRESS_KEY, VerificationStrategy.KeyRole.OTHER)); | ||
assertEquals( | ||
VerificationStrategy.Decision.INVALID, | ||
subject.maybeVerifySignature(DELEGATABLE_INACTIVE_ID_KEY, VerificationStrategy.KeyRole.OTHER)); | ||
assertEquals( | ||
VerificationStrategy.Decision.INVALID, | ||
subject.maybeVerifySignature(DELEGATABLE_INACTIVE_ADDRESS_KEY, VerificationStrategy.KeyRole.OTHER)); | ||
assertEquals( | ||
VerificationStrategy.Decision.DELEGATE_TO_CRYPTOGRAPHIC_VERIFICATION, | ||
subject.maybeVerifySignature(CRYPTO_KEY, VerificationStrategy.KeyRole.OTHER)); | ||
} | ||
|
||
@Test | ||
void doesNotSupportAmendingTransfer() { | ||
final var subject = new ActiveContractVerificationStrategy(ACTIVE_NUMBER, ACTIVE_ADDRESS, true); | ||
|
||
assertThrows( | ||
UnsupportedOperationException.class, | ||
() -> subject.maybeAmendTransfer(CryptoTransferTransactionBody.DEFAULT, List.of())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.