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

feat: Differential testing: Enhance contract bytecode dumper to handle modular representation #11523

Merged
merged 20 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,35 @@

package com.hedera.node.app.bbm;

import static com.hedera.node.app.bbm.accounts.AccountDumpUtils.dumpModAccounts;
import static com.hedera.node.app.bbm.accounts.AccountDumpUtils.dumpMonoAccounts;
import static com.hedera.node.app.bbm.associations.TokenAssociationsDumpUtils.dumpModTokenRelations;
import static com.hedera.node.app.bbm.associations.TokenAssociationsDumpUtils.dumpMonoTokenRelations;
import static com.hedera.node.app.bbm.contracts.ContractBytecodesDumpUtils.dumpModContractBytecodes;
import static com.hedera.node.app.bbm.files.FilesDumpUtils.dumpModFiles;
import static com.hedera.node.app.bbm.files.FilesDumpUtils.dumpMonoFiles;
import static com.hedera.node.app.bbm.nfts.UniqueTokenDumpUtils.dumpModUniqueTokens;
import static com.hedera.node.app.bbm.nfts.UniqueTokenDumpUtils.dumpMonoUniqueTokens;
import static com.hedera.node.app.records.BlockRecordService.BLOCK_INFO_STATE_KEY;
import static com.hedera.node.app.service.mono.state.migration.StateChildIndices.ACCOUNTS;
import static com.hedera.node.app.service.mono.state.migration.StateChildIndices.NETWORK_CTX;
import static com.hedera.node.app.service.mono.state.migration.StateChildIndices.STORAGE;
import static com.hedera.node.app.service.mono.state.migration.StateChildIndices.TOKEN_ASSOCIATIONS;
import static com.hedera.node.app.service.mono.state.migration.StateChildIndices.UNIQUE_TOKENS;
import static com.hedera.node.app.service.token.impl.TokenServiceImpl.ACCOUNTS_KEY;
import static com.hedera.node.app.service.token.impl.TokenServiceImpl.NFTS_KEY;
import static com.hedera.node.app.service.token.impl.TokenServiceImpl.TOKEN_RELS_KEY;
import static java.util.Objects.requireNonNull;

import com.hedera.hapi.node.base.AccountID;
import com.hedera.hapi.node.base.FileID;
import com.hedera.hapi.node.base.NftID;
import com.hedera.hapi.node.base.TokenAssociation;
import com.hedera.hapi.node.state.blockrecords.BlockInfo;
import com.hedera.hapi.node.state.token.Account;
import com.hedera.hapi.node.state.token.Nft;
import com.hedera.hapi.node.state.token.TokenRelation;
import com.hedera.node.app.bbm.contracts.ContractBytecodesDumpUtils;
import com.hedera.node.app.records.BlockRecordService;
import com.hedera.node.app.service.file.FileService;
import com.hedera.node.app.service.file.impl.FileServiceImpl;
Expand All @@ -61,6 +69,8 @@ public class StateDumper {
private static final String SEMANTIC_UNIQUE_TOKENS = "uniqueTokens.txt";
private static final String SEMANTIC_TOKEN_RELATIONS = "tokenRelations.txt";
private static final String SEMANTIC_FILES = "files.txt";
private static final String SEMANTIC_ACCOUNTS = "accounts.txt";
private static final String SEMANTIC_CONTRACT_BYTECODES = "contractBytecodes.txt";

public static void dumpMonoChildrenFrom(
@NonNull final MerkleHederaState state, @NonNull final DumpCheckpoint checkpoint) {
Expand All @@ -70,6 +80,13 @@ public static void dumpMonoChildrenFrom(
dumpMonoTokenRelations(
Paths.get(dumpLoc, SEMANTIC_TOKEN_RELATIONS), state.getChild(TOKEN_ASSOCIATIONS), checkpoint);
dumpMonoFiles(Paths.get(dumpLoc, SEMANTIC_FILES), state.getChild(STORAGE), checkpoint);
dumpMonoAccounts(Paths.get(dumpLoc, SEMANTIC_TOKEN_RELATIONS), state.getChild(ACCOUNTS), checkpoint);

ContractBytecodesDumpUtils.dumpMonoContractBytecodes(
Paths.get(dumpLoc, SEMANTIC_CONTRACT_BYTECODES),
state.getChild(ACCOUNTS),
state.getChild(STORAGE),
checkpoint);
}

public static void dumpModChildrenFrom(
Expand All @@ -94,6 +111,12 @@ public static void dumpModChildrenFrom(
final VirtualMap<OnDiskKey<FileID>, OnDiskValue<com.hedera.hapi.node.state.file.File>> files =
requireNonNull(state.getChild(state.findNodeIndex(FileService.NAME, FileServiceImpl.BLOBS_KEY)));
dumpModFiles(Paths.get(dumpLoc, SEMANTIC_FILES), files, checkpoint);

final VirtualMap<OnDiskKey<AccountID>, OnDiskValue<Account>> accounts =
requireNonNull(state.getChild(state.findNodeIndex(TokenService.NAME, ACCOUNTS_KEY)));
dumpModAccounts(Paths.get(dumpLoc, SEMANTIC_ACCOUNTS), accounts, checkpoint);

dumpModContractBytecodes(Paths.get(dumpLoc, SEMANTIC_CONTRACT_BYTECODES), accounts, checkpoint);
}

private static String getExtantDumpLoc(
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
/*
* Copyright (C) 2024 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.bbm.accounts;

import com.hedera.hapi.node.base.AccountID;
import com.hedera.hapi.node.base.Key;
import com.hedera.hapi.node.base.NftID;
import com.hedera.hapi.node.base.TokenID;
import com.hedera.hapi.node.state.token.Account;
import com.hedera.hapi.node.state.token.Account.StakedIdOneOfType;
import com.hedera.hapi.node.state.token.AccountApprovalForAllAllowance;
import com.hedera.hapi.node.state.token.AccountCryptoAllowance;
import com.hedera.hapi.node.state.token.AccountFungibleTokenAllowance;
import com.hedera.node.app.service.mono.pbj.PbjConverter;
import com.hedera.node.app.service.mono.state.submerkle.FcTokenAllowanceId;
import com.hedera.node.app.service.mono.state.virtual.entities.OnDiskAccount;
import com.hedera.node.app.service.mono.utils.EntityNum;
import com.hedera.node.app.service.mono.utils.EntityNumPair;
import com.hedera.node.app.state.merkle.disk.OnDiskValue;
import com.hedera.pbj.runtime.OneOf;
import com.hedera.pbj.runtime.io.buffer.Bytes;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.Map;
import java.util.Set;

public record HederaAccount(
@Nullable AccountID accountId,
@NonNull Bytes alias,
@Nullable Key key,
long expirationSecond,
long tinybarBalance,
String memo,
boolean deleted,
long stakedToMe,
long stakePeriodStart,
OneOf<StakedIdOneOfType> stakedId,
boolean declineReward,
boolean receiverSigRequired,
@Nullable TokenID headTokenId,
@Nullable NftID headNftId,
long headNftSerialNumber,
long numberOwnedNfts,
int maxAutoAssociations,
int usedAutoAssociations,
int numberAssociations,
boolean smartContract,
int numberPositiveBalances,
long ethereumNonce,
long stakeAtStartOfLastRewardedPeriod,
@Nullable AccountID autoRenewAccountId,
long autoRenewSeconds,
int contractKvPairsNumber,
@Nullable List<AccountCryptoAllowance> cryptoAllowances,
@Nullable List<AccountApprovalForAllAllowance> approveForAllNftAllowances,
@Nullable List<AccountFungibleTokenAllowance> tokenAllowances,
int numberTreasuryTitles,
boolean expiredAndPendingRemoval,
@NonNull Bytes firstContractStorageKey,
boolean immutable,
long stakedNodeAddressBookId) {

private static final AccountID MISSING_ACCOUNT_ID = AccountID.DEFAULT;
public static final long ONE_HBAR_IN_TINYBARS = 100_000_000L;
public static final HederaAccount DUMMY_ACCOUNT = new HederaAccount(
null, null, null, 0, 0, "", false, 0, 0, null, false, false, null, null, 0, 0, 0, 0, 0, false, 0, 0, 0,
null, 0, 0, null, null, null, 0, false, null, false, 0);

public static HederaAccount fromMono(OnDiskAccount account) {
return new HederaAccount(
AccountID.newBuilder().accountNum(account.getAccountNumber()).build(),
Bytes.wrap(account.getAlias().toByteArray()),
PbjConverter.asPbjKey(account.getKey()),
account.getExpiry(),
account.getBalance() * ONE_HBAR_IN_TINYBARS,
account.getMemo(),
account.isDeleted(),
account.getStakedToMe(),
account.getStakePeriodStart(),
new OneOf<>(StakedIdOneOfType.STAKED_ACCOUNT_ID, account.getStakedId()),
account.isDeclineReward(),
account.isReceiverSigRequired(),
new TokenID(0, 0, account.getHeadTokenId()),
new NftID(new TokenID(0, 0, account.getHeadNftId()), account.getHeadNftSerialNum()),
account.getHeadNftSerialNum(),
account.getNftsOwned(),
account.getMaxAutoAssociations(),
account.getUsedAutoAssociations(),
account.getNumAssociations(),
account.isSmartContract(),
account.getNumPositiveBalances(),
account.getEthereumNonce(),
account.getStakeAtStartOfLastRewardedPeriod(),
account.getAutoRenewAccount() != null
? account.getAutoRenewAccount().toPbjAccountId()
: null,
account.getAutoRenewSecs(),
account.getNumContractKvPairs(),
toCryptoAllowance(account.getCryptoAllowances()),
toApproveForAllNftAllowances(account.getApproveForAllNfts()),
toAccountFungibleTokenAllowance(account.getFungibleTokenAllowances()),
account.getNumTreasuryTitles(),
account.isExpiredAndPendingRemoval(),
toBytes(account.getFirstContractStorageKey().getKey()),
account.isImmutable(),
account.getStakedNodeAddressBookId());
}

@NonNull
private static Bytes toBytes(@NonNull final int[] packed) {
final var buf = ByteBuffer.allocate(32);
buf.asIntBuffer().put(packed);
return Bytes.wrap(buf.array());
}

private static List<AccountCryptoAllowance> toCryptoAllowance(Map<EntityNum, Long> cryptoAllowanceMap) {
return cryptoAllowanceMap.entrySet().stream()
.map(c -> new AccountCryptoAllowance(c.getKey().toEntityId().toPbjAccountId(), c.getValue()))
.toList();
}

private static List<AccountApprovalForAllAllowance> toApproveForAllNftAllowances(
Set<FcTokenAllowanceId> allowanceIds) {
return allowanceIds.stream()
.map(a -> new AccountApprovalForAllAllowance(
new TokenID(0, 0, a.getTokenNum().longValue()),
a.getSpenderNum().toEntityId().toPbjAccountId()))
.toList();
}

private static List<AccountFungibleTokenAllowance> toAccountFungibleTokenAllowance(
Map<FcTokenAllowanceId, Long> tokenAllowanceMap) {
return tokenAllowanceMap.entrySet().stream()
.map(t -> new AccountFungibleTokenAllowance(
new TokenID(0, 0, t.getKey().getTokenNum().longValue()),
t.getKey().getSpenderNum().toEntityId().toPbjAccountId(),
t.getValue()))
.toList();
}

public static HederaAccount fromMod(OnDiskValue<Account> account) {
return new HederaAccount(
account.getValue().accountId(),
account.getValue().alias(),
account.getValue().key(),
account.getValue().expirationSecond(),
account.getValue().tinybarBalance(),
account.getValue().memo(),
account.getValue().deleted(),
account.getValue().stakedToMe(),
account.getValue().stakePeriodStart(),
account.getValue().stakedId(),
account.getValue().declineReward(),
account.getValue().receiverSigRequired(),
account.getValue().headTokenId(),
account.getValue().headNftId(),
account.getValue().headNftSerialNumber(),
account.getValue().numberOwnedNfts(),
account.getValue().maxAutoAssociations(),
account.getValue().usedAutoAssociations(),
account.getValue().numberAssociations(),
account.getValue().smartContract(),
account.getValue().numberPositiveBalances(),
account.getValue().ethereumNonce(),
account.getValue().stakeAtStartOfLastRewardedPeriod(),
account.getValue().autoRenewAccountId(),
account.getValue().autoRenewSeconds(),
account.getValue().contractKvPairsNumber(),
account.getValue().cryptoAllowances(),
account.getValue().approveForAllNftAllowances(),
account.getValue().tokenAllowances(),
account.getValue().numberTreasuryTitles(),
account.getValue().expiredAndPendingRemoval(),
account.getValue().firstContractStorageKey(),
isAccountImmutable(account.getValue()),
account.getValue().stakedNodeId() != null ? account.getValue().stakedNodeId() : 0);
}

private static final AccountID immutableAccount1 =
AccountID.newBuilder().accountNum(800).build();
private static final AccountID immutableAccount2 =
AccountID.newBuilder().accountNum(801).build();
private static final List<AccountID> immutableAccounts = List.of(immutableAccount1, immutableAccount2);

private static boolean isAccountImmutable(Account account) {
return immutableAccounts.contains(account.accountId());
}

public boolean isImmutable() {
return immutable;
}

public EntityNumPair getHeadNftKey() {
if (headNftId() == null || headNftId().tokenId() == null) {
return null;
}

return EntityNumPair.fromLongs(headNftId().tokenId().tokenNum(), headNftSerialNumber);
}

public EntityNumPair getLatestAssociation() {
if (accountId == null || accountId.accountNum() == null || headTokenId() == null) {
return null;
}
return EntityNumPair.fromLongs(accountId.accountNum(), headTokenId().tokenNum());
}

public long totalStake() {
return tinybarBalance() / ONE_HBAR_IN_TINYBARS + stakedToMe();
}

public AccountID getProxy() {
return MISSING_ACCOUNT_ID;
}

public int[] getFirstUint256Key() {
return toInts(firstContractStorageKey);
}

private static int[] toInts(Bytes bytes) {
final var bytesArray = bytes.toByteArray();
ByteBuffer buf = ByteBuffer.wrap(bytesArray);
int[] ints = new int[bytesArray.length / 4];
for (int i = 0; i < ints.length; i++) {
ints[i] = buf.getInt();
}
return ints;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2024 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.bbm.contracts;

import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.Arrays;

/** Implements equality-of-content on a byte array so it can be used as a map key */
public record ByteArrayAsKey(@NonNull byte[] array) {

@Override
public boolean equals(final Object obj) {
return obj instanceof ByteArrayAsKey other && Arrays.equals(array, other.array);
}

@Override
public int hashCode() {
return Arrays.hashCode(array);
}

@Override
public String toString() {
return "ByteArrayAsKey{" + "array=" + Arrays.toString(array) + '}';
}
}
Loading
Loading