Skip to content

Commit 2d7992d

Browse files
committed
add AssetUpdateHelper
1 parent 9d6beff commit 2d7992d

10 files changed

Lines changed: 148 additions & 3 deletions

File tree

‎src/main/java/org/tron/common/application/ApplicationImpl.java‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
import org.tron.core.config.args.Args;
77
import org.tron.core.db.BlockStore;
88
import org.tron.core.db.Manager;
9-
import org.tron.core.db2.common.IRevokingDB;
10-
import org.tron.core.db2.core.SnapshotManager;
9+
import org.tron.core.db.api.AssetUpdateHelper;
1110
import org.tron.core.net.node.Node;
1211
import org.tron.core.net.node.NodeDelegate;
1312
import org.tron.core.net.node.NodeDelegateImpl;
@@ -29,6 +28,7 @@ public class ApplicationImpl implements Application {
2928

3029
private boolean isProducer;
3130

31+
3232
private void resetP2PNode() {
3333
p2pNode.listen();
3434
p2pNode.syncFrom(null);
@@ -45,6 +45,9 @@ public void init(Args args) {
4545
blockStoreDb = dbManager.getBlockStore();
4646
services = new ServiceContainer();
4747
nodeDelegate = new NodeDelegateImpl(dbManager);
48+
if (dbManager.needToUpdateAsset()) {
49+
new AssetUpdateHelper(dbManager).doWork();
50+
}
4851
}
4952

5053
@Override

‎src/main/java/org/tron/core/capsule/AccountCapsule.java‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,15 @@ public boolean addAsset(byte[] key, long value) {
390390
return true;
391391
}
392392

393+
/**
394+
* add asset.
395+
*/
396+
public boolean addAssetV2Map(Map<String, Long> assetMap) {
397+
this.account = this.account.toBuilder().putAllAssetV2(assetMap).build();
398+
return true;
399+
}
400+
401+
393402
/**
394403
* add asset.
395404
*/
@@ -402,6 +411,15 @@ public Map<String, Long> getAssetMap() {
402411
return assetMap;
403412
}
404413

414+
public Map<String, Long> getAssetV2Map() {
415+
Map<String, Long> assetMap = this.account.getAssetV2Map();
416+
if (assetMap.isEmpty()) {
417+
assetMap = Maps.newHashMap();
418+
}
419+
420+
return assetMap;
421+
}
422+
405423

406424
public long getLatestAssetOperationTime(String assetName) {
407425
return this.account.getLatestAssetOperationTimeOrDefault(assetName, 0);

‎src/main/java/org/tron/core/capsule/AssetIssueCapsule.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.nio.charset.Charset;
2121
import java.util.List;
2222
import lombok.extern.slf4j.Slf4j;
23+
import org.tron.common.utils.ByteArray;
2324
import org.tron.protos.Contract.AssetIssueContract;
2425
import org.tron.protos.Contract.AssetIssueContract.FrozenSupply;
2526

@@ -61,6 +62,16 @@ public ByteString getName() {
6162
return this.assetIssueContract.getName();
6263
}
6364

65+
public void setId(long id) {
66+
this.assetIssueContract = this.assetIssueContract.toBuilder()
67+
.setId(String.valueOf(id))
68+
.build();
69+
}
70+
71+
public long getId() {
72+
return Long.parseLong(this.assetIssueContract.getId());
73+
}
74+
6475
public void setOrder(long order) {
6576
this.assetIssueContract = this.assetIssueContract.toBuilder()
6677
.setOrder(order)
@@ -71,6 +82,9 @@ public long getOrder() {
7182
return this.assetIssueContract.getOrder();
7283
}
7384

85+
public byte[] createDbV2Key() {
86+
return ByteArray.fromLong(getId());
87+
}
7488
public byte[] createDbKey() {
7589
long order = getOrder();
7690
if (order == 0) {

‎src/main/java/org/tron/core/capsule/ExchangeCapsule.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ public byte[] getSecondTokenId() {
8080
return this.exchange.getSecondTokenId().toByteArray();
8181
}
8282

83+
public void setSecondTokenId(byte[] id) {
84+
this.exchange = this.exchange.toBuilder()
85+
.setSecondTokenId(ByteString.copyFrom(id))
86+
.build();
87+
}
88+
8389
public long getFirstTokenBalance() {
8490
return this.exchange.getFirstTokenBalance();
8591
}

‎src/main/java/org/tron/core/db/Manager.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ public WitnessStore getWitnessStore() {
192192
return this.witnessStore;
193193
}
194194

195+
public boolean needToUpdateAsset() {
196+
return getDynamicPropertiesStore().getTokenUpdateDone() == 0L;
197+
}
198+
195199
public DynamicPropertiesStore getDynamicPropertiesStore() {
196200
return this.dynamicPropertiesStore;
197201
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
}

‎src/main/protos/core/Contract.proto‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ message WitnessUpdateContract {
9595
}
9696

9797
message AssetIssueContract {
98+
string id = 41;
99+
98100
message FrozenSupply {
99101
int64 frozen_amount = 1;
100102
int64 frozen_days = 2;

‎src/main/protos/core/Tron.proto‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ message Account {
8787
map<string, int64> asset = 6;
8888

8989
// the other asset owned by this account,key is assetId
90-
map<string, int64> assetV2 = 36;
90+
map<string, int64> assetV2 = 56;
9191

9292
// the frozen balance for bandwidth
9393
repeated Frozen frozen = 7;

‎src/test/java/org/tron/core/capsule/ExchangeCapsuleTest.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,5 @@ public void testExchange() {
122122

123123
}
124124

125+
125126
}

‎src/test/java/org/tron/core/db/api/StoreAPITest.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,19 @@ public void addAssetIssueToStoreV2() {
344344
Assert.assertEquals(true, assetIssueCapsule.getName().equals(assetIssue1.getName()));
345345
}
346346

347+
@Test
348+
public void UpdateAssetV2() {
349+
long tokenIdNum = dbManager.getDynamicPropertiesStore().getTokenIdNum();
350+
351+
for (AssetIssueCapsule assetIssueCapsule : dbManager.getAssetIssueStore().getAllAssetIssues()) {
352+
dbManager.getAssetIssueV2Store().put(ByteArray.fromLong(tokenIdNum), assetIssueCapsule);
353+
assetIssueCapsule.setId(tokenIdNum);
354+
tokenIdNum++;
355+
}
356+
Assert.assertEquals(2, dbManager.getAssetIssueV2Store().getAllAssetIssues().size());
357+
358+
}
359+
347360
@Test
348361
public void getTransactionsFromThis() {
349362
List<Transaction> transactionList = storeAPI

0 commit comments

Comments
 (0)