Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove SENDER_ALIAS & SPENDER_ALIAS constants in web3 tests #10405

Merged
merged 27 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a64acef
spotlessApply + removing unnecessary methods
filev94 Feb 11, 2025
073eba1
spotlessApply + generating secp2561Key
filev94 Feb 11, 2025
1901481
Merge branch 'main' into 10319-refactor-web3-test-code
filev94 Feb 13, 2025
753adfd
spotlessApply + refactoring
filev94 Feb 13, 2025
93e8c15
refactoring spender/sender alias/public key
filev94 Feb 13, 2025
d7bd94e
refactoring methods
filev94 Feb 14, 2025
1124263
Merge branch 'main' into 10319-refactor-web3-test-code
filev94 Feb 14, 2025
0aab200
resolving conflicts + spotless
filev94 Feb 14, 2025
dded7b0
resolving conflicts + spotless
filev94 Feb 14, 2025
b57407e
small refactoring
filev94 Feb 14, 2025
76a260d
Merge branch 'main' into 10319-refactor-web3-test-code
filev94 Feb 17, 2025
293cf70
small refactoring
filev94 Feb 17, 2025
d1b9e3d
small refactoring & removing spender_alias constant
filev94 Feb 17, 2025
35c4fba
removing code smell
filev94 Feb 17, 2025
c10c032
changing to var + removing version in build.gradle
filev94 Feb 17, 2025
bb88cad
spotlessApply
filev94 Feb 17, 2025
80540d0
addressing PR comments
filev94 Feb 17, 2025
9ebda33
removing unused var
filev94 Feb 17, 2025
990f34b
removing spacing
filev94 Feb 17, 2025
b1e4f50
Merge branch 'main' into 10319-refactor-web3-test-code
filev94 Feb 18, 2025
986b7e8
small refactoring
filev94 Feb 18, 2025
6bde688
addressing PR comments
filev94 Feb 19, 2025
4a0d61c
addressing PR comments
filev94 Feb 19, 2025
fdfb1ad
Merge branch 'main' into 10319-refactor-web3-test-code
filev94 Feb 19, 2025
f2ca2f2
Merge branch 'main' into 10319-refactor-web3-test-code
filev94 Feb 20, 2025
eb8ec91
addressing PR comments
filev94 Feb 20, 2025
bc5831b
reverting testImplementation change
filev94 Feb 20, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions hedera-mirror-common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ dependencies {
api("org.apache.tuweni:tuweni-bytes")
api("org.apache.tuweni:tuweni-units")
api("org.springframework.boot:spring-boot-starter-data-jpa")
api("org.web3j:core")
testImplementation("org.hyperledger.besu:evm")
testImplementation("org.springframework.boot:spring-boot-testcontainers")
testImplementation("org.testcontainers:postgresql")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@
import jakarta.persistence.EntityManager;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.security.SecureRandom;
import java.time.Instant;
import java.time.LocalDate;
Expand All @@ -129,12 +128,15 @@
import java.util.function.Supplier;
import lombok.CustomLog;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.Value;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.support.TransactionOperations;
import org.web3j.crypto.Keys;
import org.web3j.utils.Numeric;

@Component
@CustomLog
Expand Down Expand Up @@ -1183,22 +1185,28 @@ public byte[] key() {
public byte[] key(KeyCase keyCase) {
var key =
switch (keyCase) {
case ECDSA_SECP256K1 -> Key.newBuilder()
.setECDSASecp256K1(ByteString.copyFrom(generateSecp256k1Key()));
case ECDSA_SECP256K1 -> Key.newBuilder().setECDSASecp256K1(generateSecp256k1Key());
case ED25519 -> Key.newBuilder().setEd25519(ByteString.copyFrom(bytes(KEY_LENGTH_ED25519)));
default -> throw new UnsupportedOperationException("Key type not supported");
};

return key.build().toByteArray();
}

private byte[] generateSecp256k1Key() {
byte[] xCoordinate = bytes(32); // The x-coordinate of the elliptic curve point
byte prefix = (byte) (Math.random() < 0.5 ? 0x02 : 0x03); // Randomly chosen even or odd y-coordinate
ByteBuffer compressedKey = ByteBuffer.allocate(33);
compressedKey.put(prefix);
compressedKey.put(xCoordinate);
return compressedKey.array();
@SneakyThrows
private ByteString generateSecp256k1Key() {
var keyPair = Keys.createEcKeyPair();
var publicKey = keyPair.getPublicKey();

// Convert BigInteger public key to a full 65-byte uncompressed key
var fullPublicKey = Numeric.hexStringToByteArray(Numeric.toHexStringWithPrefixZeroPadded(publicKey, 130));

// Convert to compressed format (33 bytes)
var prefix = (byte) (fullPublicKey[64] % 2 == 0 ? 0x02 : 0x03); // 0x02 for even Y, 0x03 for odd Y
var compressedKey = new byte[33];
compressedKey[0] = prefix;
System.arraycopy(fullPublicKey, 1, compressedKey, 1, 32); // Copy only X coordinate
return ByteString.copyFrom(compressedKey);
}

public long number() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@

package com.hedera.mirror.web3.service;

import static com.hedera.mirror.web3.utils.ContractCallTestUtil.SENDER_ALIAS;
import static com.hedera.mirror.web3.utils.ContractCallTestUtil.SENDER_PUBLIC_KEY;

import com.google.common.collect.Range;
import com.google.protobuf.ByteString;
import com.hedera.mirror.common.domain.balance.AccountBalance;
import com.hedera.mirror.common.domain.balance.TokenBalance;
import com.hedera.mirror.common.domain.entity.Entity;
Expand All @@ -37,8 +33,8 @@
import com.hedera.mirror.web3.viewmodel.BlockType;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import org.apache.commons.lang3.tuple.Pair;
import org.hyperledger.besu.datatypes.Address;

public abstract class AbstractContractCallServiceHistoricalTest extends AbstractContractCallServiceTest {

Expand Down Expand Up @@ -76,19 +72,9 @@ protected void tokenAccountFrozenRelationshipPersistHistorical(
.persist();
}

protected Entity accountEntityPersistHistorical(final Range<Long> timestampRange) {
return domainBuilder
.entity()
.customize(e -> e.type(EntityType.ACCOUNT)
.balance(1_000_000_000_000L)
.timestampRange(timestampRange)
.createdTimestamp(timestampRange.lowerEndpoint()))
.persist();
}

protected Pair<Entity, Entity> accountTokenAndFrozenRelationshipPersistHistorical(
final Range<Long> historicalRange) {
final var account = accountEntityWithAliasPersistHistorical(historicalRange);
final var account = accountEntityPersistWithEvmAddressHistorical(historicalRange);
final var tokenEntity = tokenEntityPersistHistorical(historicalRange);
fungibleTokenPersistHistorical(tokenEntity, historicalRange);
domainBuilder
Expand All @@ -103,48 +89,28 @@ protected Pair<Entity, Entity> accountTokenAndFrozenRelationshipPersistHistorica
return Pair.of(account, tokenEntity);
}

protected Entity accountEntityNoEvmAddressPersistHistorical(final Range<Long> timestampRange) {
return domainBuilder
.entity()
.customize(e -> e.type(EntityType.ACCOUNT)
.evmAddress(null)
.alias(null)
.balance(1_000_000_000_000L)
.timestampRange(timestampRange)
.createdTimestamp(timestampRange.lowerEndpoint()))
.persist();
}

protected Entity accountEntityWithAliasPersistHistorical(final Range<Long> timestampRange) {
return accountEntityWithAliasPersistHistorical(SENDER_ALIAS, SENDER_PUBLIC_KEY, timestampRange);
}
protected Entity accountEntityPersistHistoricalCustomizable(
final Range<Long> timestampRange, Consumer<Entity.EntityBuilder<?, ?>> customizer) {

protected Entity accountEntityWithAliasPersistHistorical(
final Address evmAddress, final ByteString alias, final Range<Long> timestampRange) {
return domainBuilder
.entity()
.customize(e -> e.type(EntityType.ACCOUNT)
.alias(alias.toByteArray())
.evmAddress(evmAddress.toArray())
.balance(1_000_000_000_000L)
.createdTimestamp(timestampRange.lowerEndpoint())
.timestampRange(timestampRange))
.customize(e -> {
e.type(EntityType.ACCOUNT)
.balance(DEFAULT_ACCOUNT_BALANCE)
.createdTimestamp(timestampRange.lowerEndpoint())
.timestampRange(timestampRange);
customizer.accept(e);
})
.persist();
}

protected Entity accountEntityNoEvmAddressWithBalancePersistHistorical(
final long balance, final Range<Long> timestampRange) {
final var entity = domainBuilder
.entity()
.customize(e -> e.balance(balance)
.alias(null)
.evmAddress(null)
.timestampRange(timestampRange)
.createdTimestamp(timestampRange.lowerEndpoint()))
.persist();
accountBalancePersistHistorical(entity.toEntityId(), balance, timestampRange);
protected Entity accountEntityPersistWithEvmAddressHistorical(final Range<Long> timestampRange) {
return accountEntityPersistHistoricalCustomizable(timestampRange, e -> {});
}

return entity;
protected Entity accountEntityPersistHistorical(final Range<Long> timestampRange) {
return accountEntityPersistHistoricalCustomizable(
timestampRange, e -> e.evmAddress(null).alias(null));
}

protected void accountBalancePersistHistorical(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

import com.google.common.collect.Range;
import com.google.protobuf.ByteString;
import com.hedera.mirror.common.domain.balance.AccountBalance;
import com.hedera.mirror.common.domain.balance.TokenBalance;
import com.hedera.mirror.common.domain.entity.Entity;
Expand Down Expand Up @@ -374,17 +373,6 @@ protected Entity accountEntityWithEvmAddressPersist() {
return accountEntityPersistCustomizable(e -> e.type(EntityType.ACCOUNT).balance(DEFAULT_ACCOUNT_BALANCE));
}

/**
*
* @param alias - the alias with which the account is created
* @param publicKey - the public key with which the account is created
* @return Entity object that is persisted in the db
*/
protected Entity accountPersistWithAlias(final Address alias, final ByteString publicKey) {
return accountEntityPersistCustomizable(
e -> e.evmAddress(alias.toArray()).alias(publicKey.toByteArray()));
}

/**
*
* @param customizer - the consumer with which to customize the entity
Expand Down Expand Up @@ -496,12 +484,20 @@ protected Pair<Entity, Entity> persistTokenWithAutoRenewAndTreasuryAccounts(
return Pair.of(tokenToUpdateEntity, autoRenewAccount);
}

protected String getAddressFromEntity(Entity entity) {
protected String getAddressFromEntity(final Entity entity) {
return EvmTokenUtils.toAddress(entity.toEntityId()).toHexString();
}

protected String getAliasFromEntity(Entity entity) {
return Bytes.wrap(entity.getEvmAddress()).toHexString();
protected String getAliasFromEntity(final Entity entity) {
return getEvmAddressBytesFromEntity(entity).toHexString();
}

protected Bytes getEvmAddressBytesFromEntity(final Entity entity) {
return Bytes.wrap(entity.getEvmAddress());
}

protected Address getAliasAddressFromEntity(final Entity entity) {
return Address.wrap(getEvmAddressBytesFromEntity(entity));
}

protected ContractDebugParameters getDebugParameters(
Expand Down
Loading
Loading