|
| 1 | +package org.tron.core.db.api; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Iterator; |
| 5 | +import java.util.Map; |
| 6 | +import java.util.Map.Entry; |
| 7 | +import org.tron.common.utils.ByteArray; |
| 8 | +import org.tron.core.capsule.AccountCapsule; |
| 9 | +import org.tron.core.capsule.AssetIssueCapsule; |
| 10 | +import org.tron.core.capsule.ExchangeCapsule; |
| 11 | +import org.tron.core.db.Manager; |
| 12 | + |
| 13 | + |
| 14 | +public class AssetUpdateHelper { |
| 15 | + |
| 16 | + private Manager dbManager; |
| 17 | + |
| 18 | + public AssetUpdateHelper(Manager dbManager) { |
| 19 | + this.dbManager = dbManager; |
| 20 | + } |
| 21 | + |
| 22 | + public void doWork() { |
| 23 | + |
| 24 | + updateAsset(); |
| 25 | + updateExchange(); |
| 26 | + updateAccount(); |
| 27 | + finish(); |
| 28 | + } |
| 29 | + |
| 30 | + public void updateAsset() { |
| 31 | + |
| 32 | + long tokenIdNum = dbManager.getDynamicPropertiesStore().getTokenIdNum(); |
| 33 | + |
| 34 | + for (AssetIssueCapsule assetIssueCapsule : dbManager.getAssetIssueStore().getAllAssetIssues()) { |
| 35 | + assetIssueCapsule.setId(tokenIdNum); |
| 36 | + dbManager.getAssetIssueV2Store().put(assetIssueCapsule.createDbV2Key(), assetIssueCapsule); |
| 37 | + dbManager.getAssetIssueStore().put(assetIssueCapsule.createDbKey(), assetIssueCapsule); |
| 38 | + tokenIdNum++; |
| 39 | + } |
| 40 | + |
| 41 | + } |
| 42 | + |
| 43 | + public void updateExchange() { |
| 44 | + |
| 45 | + for (ExchangeCapsule exchangeCapsule : dbManager.getExchangeStore().getAllExchanges()) { |
| 46 | + |
| 47 | + AssetIssueCapsule firstAssetIssueCapsule = dbManager.getAssetIssueStore() |
| 48 | + .get(exchangeCapsule.getFirstTokenId()); |
| 49 | + exchangeCapsule.setSecondTokenId(ByteArray.fromLong(firstAssetIssueCapsule.getId())); |
| 50 | + |
| 51 | + AssetIssueCapsule secondAssetIssueCapsule = dbManager.getAssetIssueStore() |
| 52 | + .get(exchangeCapsule.getSecondTokenId()); |
| 53 | + exchangeCapsule.setSecondTokenId(ByteArray.fromLong(secondAssetIssueCapsule.getId())); |
| 54 | + |
| 55 | + dbManager.getExchangeV2Store().put(exchangeCapsule.createDbKey(), exchangeCapsule); |
| 56 | + } |
| 57 | + |
| 58 | + } |
| 59 | + |
| 60 | + public void updateAccount() { |
| 61 | + |
| 62 | + Iterator<Entry<byte[], AccountCapsule>> iterator = dbManager.getAccountStore().iterator(); |
| 63 | + while (iterator.hasNext()) { |
| 64 | + AccountCapsule accountCapsule = iterator.next().getValue(); |
| 65 | + HashMap<String, Long> assetV2Map = new HashMap<>(); |
| 66 | + for (Map.Entry<String, Long> entry : accountCapsule.getAssetMap().entrySet()) { |
| 67 | + AssetIssueCapsule assetIssueCapsule = dbManager.getAssetIssueStore() |
| 68 | + .get(ByteArray.fromString(entry.getKey())); |
| 69 | + assetV2Map.put(ByteArray.toStr(assetIssueCapsule.createDbV2Key()), entry.getValue()); |
| 70 | + } |
| 71 | + |
| 72 | + accountCapsule.addAssetV2Map(assetV2Map); |
| 73 | + dbManager.getAccountStore().put(accountCapsule.createDbKey(), accountCapsule); |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + public void finish() { |
| 80 | + dbManager.getDynamicPropertiesStore().saveTokenUpdateDone(1); |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | +} |
0 commit comments