Skip to content

Commit 4d80bfa

Browse files
committed
contract: add asset issue contract
1 parent 2bd9607 commit 4d80bfa

6 files changed

Lines changed: 267 additions & 7 deletions

File tree

src/main/java/org/tron/core/actuator/ActuatorFactory.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,20 @@ public static ActuatorFactory getInstance() {
2323
return INSTANCE;
2424
}
2525

26+
/**
27+
* create actuator.
28+
*/
2629
public static List<Actuator> createActuator(TransactionCapsule transactionCapsule,
2730
Manager manager) {
2831
List<Actuator> actuatorList = Lists.newArrayList();
2932
if (null == transactionCapsule || null == transactionCapsule.getInstance()) {
3033
logger.info("transactionCapsule or Transaction is null");
3134
return actuatorList;
3235
}
33-
// if (null == manager) {
34-
// logger.info("manager is null");
35-
// return actuatorList;
36-
// }
36+
// if (null == manager) {
37+
// logger.info("manager is null");
38+
// return actuatorList;
39+
// }
3740
Preconditions.checkNotNull(manager, "manager is null");
3841
Protocol.Transaction.raw rawData = transactionCapsule.getInstance().getRawData();
3942
if (TransactionType.ContractType.equals(rawData.getType())) {
@@ -58,7 +61,7 @@ private static Actuator getActuatorByContract(Contract contract, Manager manager
5861
case WitnessCreateContract:
5962
return new WitnessCreateActuator(contract.getParameter(), manager);
6063
case AssetIssueContract:
61-
break;
64+
return new AssetIssueActuator(contract.getParameter(), manager);
6265
case DeployContract:
6366
break;
6467
default:
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* java-tron is free software: you can redistribute it and/or modify
3+
* it under the terms of the GNU General Public License as published by
4+
* the Free Software Foundation, either version 3 of the License, or
5+
* (at your option) any later version.
6+
*
7+
* java-tron is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* GNU General Public License for more details.
11+
*
12+
* You should have received a copy of the GNU General Public License
13+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
14+
*/
15+
16+
package org.tron.core.actuator;
17+
18+
import com.google.common.base.Preconditions;
19+
import com.google.protobuf.Any;
20+
import com.google.protobuf.ByteString;
21+
import com.google.protobuf.InvalidProtocolBufferException;
22+
import org.slf4j.Logger;
23+
import org.slf4j.LoggerFactory;
24+
import org.tron.core.capsule.AssetIssueCapsule;
25+
import org.tron.core.db.Manager;
26+
import org.tron.core.exception.ContractExeException;
27+
import org.tron.core.exception.ContractValidateException;
28+
import org.tron.protos.Contract.AssetIssueContract;
29+
30+
public class AssetIssueActuator extends AbstractActuator {
31+
32+
private static final Logger logger = LoggerFactory.getLogger("AssetIssueActuator");
33+
34+
AssetIssueActuator(Any contract, Manager dbManager) {
35+
super(contract, dbManager);
36+
}
37+
38+
@Override
39+
public boolean execute() throws ContractExeException {
40+
try {
41+
AssetIssueContract assetIssueContract = contract.unpack(AssetIssueContract.class);
42+
43+
AssetIssueCapsule assetIssueCapsule = new AssetIssueCapsule(assetIssueContract);
44+
45+
dbManager.getAssetIssueStore()
46+
.put(assetIssueCapsule.getName().toByteArray(), assetIssueCapsule);
47+
} catch (InvalidProtocolBufferException e) {
48+
throw new ContractExeException();
49+
}
50+
return false;
51+
}
52+
53+
@Override
54+
public boolean validate() throws ContractValidateException {
55+
if (!this.contract.is(AssetIssueContract.class)) {
56+
throw new ContractValidateException();
57+
}
58+
59+
try {
60+
final AssetIssueContract assetIssueContract = this.contract.unpack(AssetIssueContract.class);
61+
62+
Preconditions.checkNotNull(assetIssueContract.getOwnerAddress(), "OwnerAddress is null");
63+
Preconditions.checkNotNull(assetIssueContract.getName(), "name is null");
64+
65+
if (this.dbManager.getAssetIssueStore().get(assetIssueContract.getName().toByteArray())
66+
!= null) {
67+
throw new ContractValidateException();
68+
}
69+
70+
} catch (InvalidProtocolBufferException e) {
71+
throw new ContractValidateException();
72+
}
73+
74+
return false;
75+
}
76+
77+
@Override
78+
public ByteString getOwnerAddress() {
79+
return null;
80+
}
81+
82+
@Override
83+
public long calcFee() {
84+
return 0;
85+
}
86+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* java-tron is free software: you can redistribute it and/or modify
3+
* it under the terms of the GNU General Public License as published by
4+
* the Free Software Foundation, either version 3 of the License, or
5+
* (at your option) any later version.
6+
*
7+
* java-tron is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* GNU General Public License for more details.
11+
*
12+
* You should have received a copy of the GNU General Public License
13+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
14+
*/
15+
16+
package org.tron.core.capsule;
17+
18+
import com.google.protobuf.ByteString;
19+
import com.google.protobuf.InvalidProtocolBufferException;
20+
import org.slf4j.Logger;
21+
import org.slf4j.LoggerFactory;
22+
import org.tron.protos.Contract.AssetIssueContract;
23+
24+
public class AssetIssueCapsule implements ProtoCapsule<AssetIssueContract> {
25+
26+
protected static final Logger logger = LoggerFactory.getLogger("AssetIssueCapsule");
27+
28+
private AssetIssueContract assetIssueContract;
29+
30+
/**
31+
* get asset issue contract from bytes data.
32+
*/
33+
public AssetIssueCapsule(byte[] data) {
34+
try {
35+
this.assetIssueContract = AssetIssueContract.parseFrom(data);
36+
} catch (InvalidProtocolBufferException e) {
37+
logger.debug(e.getMessage());
38+
}
39+
}
40+
41+
public AssetIssueCapsule(AssetIssueContract assetIssueContract) {
42+
this.assetIssueContract = assetIssueContract;
43+
}
44+
45+
public byte[] getData() {
46+
return this.assetIssueContract.toByteArray();
47+
}
48+
49+
@Override
50+
public AssetIssueContract getInstance() {
51+
return this.assetIssueContract;
52+
}
53+
54+
@Override
55+
public String toString() {
56+
return this.assetIssueContract.toString();
57+
}
58+
59+
public ByteString getName() {
60+
return this.assetIssueContract.getName();
61+
}
62+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package org.tron.core.db;
2+
3+
import java.util.List;
4+
import java.util.stream.Collectors;
5+
import org.apache.commons.lang3.ArrayUtils;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import org.tron.common.utils.ByteArray;
9+
import org.tron.core.capsule.AssetIssueCapsule;
10+
11+
public class AssetIssueStore extends TronDatabase<AssetIssueCapsule> {
12+
13+
private static final Logger logger = LoggerFactory.getLogger("AssetIssueStore");
14+
private static AssetIssueStore instance;
15+
16+
17+
private AssetIssueStore(String dbName) {
18+
super(dbName);
19+
}
20+
21+
@Override
22+
public void put(byte[] key, AssetIssueCapsule item) {
23+
logger.info("asset issue is {}, asset issue is {}", key, item);
24+
25+
byte[] value = dbSource.getData(key);
26+
if (ArrayUtils.isNotEmpty(value)) {
27+
onModify(key, value);
28+
}
29+
30+
logger.info("name is {} ", ByteArray.toHexString(key));
31+
dbSource.putData(key, item.getData());
32+
33+
if (ArrayUtils.isEmpty(value)) {
34+
onCreate(key);
35+
}
36+
}
37+
38+
/**
39+
* create fun.
40+
*
41+
* @param dbName the name of database
42+
*/
43+
public static AssetIssueStore create(String dbName) {
44+
if (instance == null) {
45+
synchronized (AssetIssueStore.class) {
46+
if (instance == null) {
47+
instance = new AssetIssueStore(dbName);
48+
}
49+
}
50+
}
51+
return instance;
52+
}
53+
54+
@Override
55+
public void delete(byte[] key) {
56+
// This should be called just before an object is removed.
57+
onDelete(key);
58+
dbSource.deleteData(key);
59+
}
60+
61+
@Override
62+
public AssetIssueCapsule get(byte[] key) {
63+
byte[] value = dbSource.getData(key);
64+
return ArrayUtils.isEmpty(value) ? null : new AssetIssueCapsule(value);
65+
}
66+
67+
/**
68+
* isAssetIssusExist fun.
69+
*
70+
* @param key the address of Account
71+
*/
72+
@Override
73+
public boolean has(byte[] key) {
74+
byte[] assetIssue = dbSource.getData(key);
75+
logger.info("name is {}, asset issue is {}", key, assetIssue);
76+
return null != assetIssue;
77+
}
78+
79+
/**
80+
* get all asset issues.
81+
*/
82+
public List<AssetIssueCapsule> getAllAssetIssues() {
83+
return dbSource.allKeys().stream()
84+
.map(this::get)
85+
.collect(Collectors.toList());
86+
}
87+
}

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public class Manager {
5151
private BlockStore blockStore;
5252
private UtxoStore utxoStore;
5353
private WitnessStore witnessStore;
54+
private AssetIssueStore assetIssueStore;
5455
private DynamicPropertiesStore dynamicPropertiesStore;
5556
private BlockCapsule genesisBlock;
5657

@@ -156,6 +157,7 @@ public void init() {
156157
this.setBlockStore(BlockStore.create("block"));
157158
this.setUtxoStore(UtxoStore.create("utxo"));
158159
this.setWitnessStore(WitnessStore.create("witness"));
160+
this.setAssetIssueStore(AssetIssueStore.create("asset-issue"));
159161
this.setDynamicPropertiesStore(DynamicPropertiesStore.create("properties"));
160162

161163
this.numHashCache = new LevelDbDataSourceImpl(
@@ -296,7 +298,7 @@ public boolean pushTransactions(final TransactionCapsule trx)
296298
pendingTrxs.add(trx);
297299
tmpDialog.merge();
298300
} catch (RevokingStoreIllegalStateException e) {
299-
301+
e.printStackTrace();
300302
}
301303
getTransactionStore().dbSource.putData(trx.getTransactionId().getBytes(), trx.getData());
302304
return true;
@@ -574,14 +576,17 @@ private void processMaintenance() {
574576
}
575577

576578

579+
/**
580+
* update signed witness.
581+
*/
577582
public void updateSignedWitness(BlockCapsule block) {
578583
//TODO: add verification
579584
WitnessCapsule witnessCapsule = witnessStore
580585
.get(block.getInstance().getBlockHeader().getRawData().getWitnessAddress().toByteArray());
581586

582587
long latestSlotNum = 0L;
583588

584-
// dynamicPropertiesStore.current_aslot + getSlotAtTime(new DateTime(block.getTimeStamp()));
589+
// dynamicPropertiesStore.current_aslot + getSlotAtTime(new DateTime(block.getTimeStamp()));
585590

586591
witnessCapsule.getInstance().toBuilder().setLatestBlockNum(block.getNum())
587592
.setLatestSlotNum(latestSlotNum)
@@ -598,6 +603,9 @@ private long blockInterval() {
598603
return LOOP_INTERVAL; // millisecond todo getFromDb
599604
}
600605

606+
/**
607+
* get slot at time.
608+
*/
601609
public long getSlotAtTime(DateTime when) {
602610
DateTime firstSlotTime = getSlotTime(1);
603611
if (when.isBefore(firstSlotTime)) {
@@ -607,6 +615,9 @@ public long getSlotAtTime(DateTime when) {
607615
}
608616

609617

618+
/**
619+
* get slot time.
620+
*/
610621
public DateTime getSlotTime(long slotNum) {
611622
if (slotNum == 0) {
612623
return DateTime.now();
@@ -703,4 +714,12 @@ public void updateWitness() {
703714
this.wits = witnessCapsuleList.subList(0, MAX_ACTIVE_WITNESS_NUM);
704715
}
705716
}
717+
718+
public AssetIssueStore getAssetIssueStore() {
719+
return assetIssueStore;
720+
}
721+
722+
public void setAssetIssueStore(AssetIssueStore assetIssueStore) {
723+
this.assetIssueStore = assetIssueStore;
724+
}
706725
}

src/main/resources/logback.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,14 @@
7878
<logger name="TransactionUtil" level="INFO"/>
7979
<logger name="TransactionVoteActuator" level="INFO"/>
8080
<logger name="WitnessStore" level="INFO"/>
81+
<logger name="AssetIssueStore" level="INFO"/>
8182
<logger name="ActuatorFactory" level="INFO"/>
8283
<logger name="ByteArray" level="INFO"/>
8384
<logger name="Args" level="INFO"/>
8485
<logger name="WitnessCteateActuator" level="INFO"/>
86+
<logger name="AssetIssueActuator" level="INFO"/>
8587
<logger name="AccountCapsule" level="INFO"/>
88+
<logger name="AssetIssueCapsule" level="INFO"/>
8689
<logger name="Manager" level="INFO"/>
8790

8891
</configuration>

0 commit comments

Comments
 (0)