Skip to content

Commit

Permalink
merge from upstream
Browse files Browse the repository at this point in the history
Signed-off-by: Anthony Petrov <[email protected]>
  • Loading branch information
anthony-swirldslabs committed Feb 28, 2024
2 parents a835854 + be1256a commit d3a4316
Show file tree
Hide file tree
Showing 31 changed files with 1,973 additions and 373 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,31 @@

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.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.records.BlockRecordService;
Expand All @@ -61,6 +67,7 @@ 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";

public static void dumpMonoChildrenFrom(
@NonNull final MerkleHederaState state, @NonNull final DumpCheckpoint checkpoint) {
Expand All @@ -70,6 +77,7 @@ 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);
}

public static void dumpModChildrenFrom(
Expand All @@ -94,6 +102,10 @@ 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);
}

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;
}
}
Loading

0 comments on commit d3a4316

Please sign in to comment.