Skip to content

Commit c75b619

Browse files
committed
Merge branch 'feature/develop_zksnark_2' of https://github.com/tronprotocol/java-tron into feature/develop_zksnark_2
2 parents 0fac007 + 2150332 commit c75b619

6 files changed

Lines changed: 299 additions & 217 deletions

File tree

‎src/main/java/org/tron/core/actuator/ShieldedTransferActuator.java‎

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.google.protobuf.InvalidProtocolBufferException;
66
import com.sun.jna.Pointer;
77
import java.util.Arrays;
8+
import java.util.HashSet;
89
import java.util.List;
910
import lombok.extern.slf4j.Slf4j;
1011
import org.apache.commons.collections4.CollectionUtils;
@@ -68,6 +69,11 @@ public boolean execute(TransactionResultCapsule ret)
6869
shieldedTransferContract.getToAmount());
6970
}
7071
ret.setStatus(fee, code.SUCESS);
72+
long totalShieldedPoolValue = dbManager.getDynamicPropertiesStore().getTotalShieldedPoolValue();
73+
long valueBalance = shieldedTransferContract.getToAmount() -
74+
shieldedTransferContract.getFromAmount() + fee;
75+
totalShieldedPoolValue -= valueBalance;
76+
dbManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(totalShieldedPoolValue);
7177
return true;
7278
}
7379

@@ -162,13 +168,18 @@ public boolean validate() throws ContractValidateException {
162168
List<SpendDescription> spendDescriptions = shieldedTransferContract.getSpendDescriptionList();
163169
// check duplicate sapling nullifiers
164170
if (CollectionUtils.isNotEmpty(spendDescriptions)) {
171+
HashSet<ByteString> nfSet = new HashSet<>();
165172
for (SpendDescription spendDescription : spendDescriptions) {
173+
if (nfSet.contains(spendDescription.getNullifier())) {
174+
throw new ContractValidateException("duplicate sapling nullifiers in this transaction");
175+
}
176+
nfSet.add(spendDescription.getNullifier());
166177
if (!dbManager.getMerkleContainer()
167178
.merkleRootExist(spendDescription.getAnchor().toByteArray())) {
168179
throw new ContractValidateException("Rt is invalid.");
169180
}
170181
if (dbManager.getNullfierStore().has(spendDescription.getNullifier().toByteArray())) {
171-
throw new ContractValidateException("duplicate sapling nullifiers in this transaction");
182+
throw new ContractValidateException("note has been spend in this transaction");
172183
}
173184
}
174185
}
@@ -185,13 +196,21 @@ public boolean validate() throws ContractValidateException {
185196
|| CollectionUtils.isNotEmpty(receiveDescriptions)) {
186197
Pointer ctx = Librustzcash.librustzcashSaplingVerificationCtxInit();
187198
for (SpendDescription spendDescription : spendDescriptions) {
199+
if (spendDescription.getValueCommitment().isEmpty()
200+
|| spendDescription.getAnchor().isEmpty()
201+
|| spendDescription.getNullifier().isEmpty()
202+
|| spendDescription.getZkproof().isEmpty()
203+
|| spendDescription.getSpendAuthoritySignature().isEmpty()) {
204+
Librustzcash.librustzcashSaplingVerificationCtxFree(ctx);
205+
throw new ContractValidateException("spend description null");
206+
}
188207
if (!Librustzcash.librustzcashSaplingCheckSpend(
189208
ctx,
190209
spendDescription.getValueCommitment().toByteArray(),
191210
spendDescription.getAnchor().toByteArray(),
192211
spendDescription.getNullifier().toByteArray(),
193212
spendDescription.getRk().toByteArray(),
194-
spendDescription.getZkproof().getValues().toByteArray(),
213+
spendDescription.getZkproof().toByteArray(),
195214
spendDescription.getSpendAuthoritySignature().toByteArray(),
196215
signHash
197216
)) {
@@ -201,12 +220,21 @@ public boolean validate() throws ContractValidateException {
201220
}
202221

203222
for (ReceiveDescription receiveDescription : receiveDescriptions) {
223+
if (receiveDescription.getValueCommitment().isEmpty()
224+
|| receiveDescription.getNoteCommitment().isEmpty()
225+
|| receiveDescription.getEpk().isEmpty()
226+
|| receiveDescription.getZkproof().isEmpty()
227+
|| receiveDescription.getCEnc().isEmpty()
228+
|| receiveDescription.getCOut().isEmpty()) {
229+
Librustzcash.librustzcashSaplingVerificationCtxFree(ctx);
230+
throw new ContractValidateException("receive description null");
231+
}
204232
if (!Librustzcash.librustzcashSaplingCheckOutput(
205233
ctx,
206234
receiveDescription.getValueCommitment().toByteArray(),
207235
receiveDescription.getNoteCommitment().toByteArray(),
208236
receiveDescription.getEpk().toByteArray(),
209-
receiveDescription.getZkproof().getValues().toByteArray()
237+
receiveDescription.getZkproof().toByteArray()
210238
)) {
211239
Librustzcash.librustzcashSaplingVerificationCtxFree(ctx);
212240
throw new ContractValidateException("librustzcashSaplingCheckOutput error");
@@ -215,6 +243,12 @@ public boolean validate() throws ContractValidateException {
215243

216244
long valueBalance = shieldedTransferContract.getToAmount() -
217245
shieldedTransferContract.getFromAmount() + fee;
246+
long totalShieldedPoolValue = dbManager.getDynamicPropertiesStore()
247+
.getTotalShieldedPoolValue();
248+
totalShieldedPoolValue -= valueBalance;
249+
if (totalShieldedPoolValue < 0) {
250+
throw new ContractValidateException("shieldedPoolValue error");
251+
}
218252
if (!Librustzcash.librustzcashSaplingFinalCheck(
219253
ctx,
220254
valueBalance,
@@ -274,10 +308,15 @@ private boolean validateTransparent(ShieldedTransferContract shieldedTransferCon
274308
if (hasTransparentFrom && !Wallet.addressValid(ownerAddress)) {
275309
throw new ContractValidateException("Invalid transparent_from_address");
276310
}
311+
if (!hasTransparentFrom && fromAmount != 0) {
312+
throw new ContractValidateException("no transparent_from_address, from_amount should be 0");
313+
}
277314
if (hasTransparentTo && !Wallet.addressValid(toAddress)) {
278315
throw new ContractValidateException("Invalid transparent_to_address");
279316
}
280-
317+
if (!hasTransparentTo && toAmount != 0) {
318+
throw new ContractValidateException("no transparent_to_address, to_amount should be 0");
319+
}
281320
if (hasTransparentFrom && hasTransparentTo && Arrays.equals(toAddress, ownerAddress)) {
282321
throw new ContractValidateException("Can't transfer trx to yourself");
283322
}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.google.protobuf.InvalidProtocolBufferException;
55
import lombok.extern.slf4j.Slf4j;
66
import org.tron.core.capsule.ProtoCapsule;
7-
import org.tron.protos.Contract.GrothProof;
87
import org.tron.protos.Contract.ReceiveDescription;
98

109
@Slf4j(topic = "capsule")
@@ -34,7 +33,7 @@ public ReceiveDescriptionCapsule(
3433
ByteString ephemeralKey,
3534
ByteString encCiphertext,
3635
ByteString outCiphertext,
37-
GrothProof zkproof) {
36+
ByteString zkproof) {
3837
this.receiveDescription =
3938
ReceiveDescription.newBuilder()
4039
.setValueCommitment(cv)
@@ -112,16 +111,16 @@ public void setNoteCommitment(ByteString bytes) {
112111
this.receiveDescription = this.receiveDescription.toBuilder().setNoteCommitment(bytes).build();
113112
}
114113

115-
public GrothProof getZkproof() {
114+
public ByteString getZkproof() {
116115
return this.receiveDescription.getZkproof();
117116
}
118117

119118
public void setZkproof(byte[] proof) {
120-
GrothProof proof1 = GrothProof.newBuilder().setValues(ByteString.copyFrom(proof)).build();
119+
ByteString proof1 = ByteString.copyFrom(proof);
121120
this.receiveDescription = this.receiveDescription.toBuilder().setZkproof(proof1).build();
122121
}
123122

124-
public void setZkproof(GrothProof proof) {
123+
public void setZkproof(ByteString proof) {
125124
this.receiveDescription = this.receiveDescription.toBuilder().setZkproof(proof).build();
126125
}
127126

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.google.protobuf.InvalidProtocolBufferException;
55
import lombok.extern.slf4j.Slf4j;
66
import org.tron.core.capsule.ProtoCapsule;
7-
import org.tron.protos.Contract.GrothProof;
87
import org.tron.protos.Contract.SpendDescription;
98

109
@Slf4j(topic = "capsule")
@@ -33,7 +32,7 @@ public SpendDescriptionCapsule(
3332
ByteString anchor,
3433
ByteString nf,
3534
ByteString rk,
36-
GrothProof zkproof,
35+
ByteString zkproof,
3736
ByteString sig) {
3837
this.spendDescription =
3938
SpendDescription.newBuilder()
@@ -98,16 +97,16 @@ public void setRk(ByteString bytes) {
9897
this.spendDescription = this.spendDescription.toBuilder().setRk(bytes).build();
9998
}
10099

101-
public GrothProof getZkproof() {
100+
public ByteString getZkproof() {
102101
return this.spendDescription.getZkproof();
103102
}
104103

105104
public void setZkproof(byte[] proof) {
106-
GrothProof proof1 = GrothProof.newBuilder().setValues(ByteString.copyFrom(proof)).build();
105+
ByteString proof1 = ByteString.copyFrom(proof);
107106
this.spendDescription = this.spendDescription.toBuilder().setZkproof(proof1).build();
108107
}
109108

110-
public void setZkproof(GrothProof proof) {
109+
public void setZkproof(ByteString proof) {
111110
this.spendDescription = this.spendDescription.toBuilder().setZkproof(proof).build();
112111
}
113112

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ private static class DynamicResourceProperties {
108108
.getBytes();
109109

110110
private static final byte[] SHIELDED_TRANSACTION_FEE = "SHIELDED_TRANSACTION_FEE".getBytes();
111+
//This value should be not negative
112+
private static final byte[] TOTAL_SHIELDED_POOL_VALUE = "TOTAL_SHIELDED_POOL_VALUE".getBytes();
111113

112114
private static final byte[] EXCHANGE_CREATE_FEE = "EXCHANGE_CREATE_FEE".getBytes();
113115

@@ -173,6 +175,9 @@ private static class DynamicResourceProperties {
173175
private static final byte[] ACTIVE_DEFAULT_OPERATIONS = "ACTIVE_DEFAULT_OPERATIONS".getBytes();
174176

175177

178+
179+
180+
176181
@Autowired
177182
private DynamicPropertiesStore(@Value("properties") String dbName) {
178183
super(dbName);
@@ -393,6 +398,12 @@ private DynamicPropertiesStore(@Value("properties") String dbName) {
393398
this.saveShieldedTransactionFee(10_000_000L); // 10TRX
394399
}
395400

401+
try {
402+
this.getTotalShieldedPoolValue();
403+
} catch (IllegalArgumentException e) {
404+
this.saveTotalShieldedPoolValue(0L); // 0
405+
}
406+
396407
try {
397408
this.getCreateNewAccountFeeInSystemContract();
398409
} catch (IllegalArgumentException e) {
@@ -1031,6 +1042,19 @@ public void saveShieldedTransactionFee(long fee) {
10311042
new BytesCapsule(ByteArray.fromLong(fee)));
10321043
}
10331044

1045+
public long getTotalShieldedPoolValue() {
1046+
return Optional.ofNullable(getUnchecked(TOTAL_SHIELDED_POOL_VALUE))
1047+
.map(BytesCapsule::getData)
1048+
.map(ByteArray::toLong)
1049+
.orElseThrow(
1050+
() -> new IllegalArgumentException("not found TOTAL_SHIELDED_POOL_Value"));
1051+
}
1052+
1053+
public void saveTotalShieldedPoolValue(long value) {
1054+
this.put(TOTAL_SHIELDED_POOL_VALUE,
1055+
new BytesCapsule(ByteArray.fromLong(value)));
1056+
}
1057+
10341058
public long getCreateAccountFee() {
10351059
return Optional.ofNullable(getUnchecked(CREATE_ACCOUNT_FEE))
10361060
.map(BytesCapsule::getData)

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -302,17 +302,12 @@ message IncrementalMerkleVoucherInfo {
302302
repeated bytes paths = 2;
303303
}
304304

305-
306-
message GrothProof {
307-
bytes values = 1;
308-
}
309-
310305
message SpendDescription {
311306
bytes value_commitment = 1;
312307
bytes anchor = 2; // merkle root
313308
bytes nullifier = 3; // used for check double spend
314309
bytes rk = 4; // used for check spend authority signature
315-
GrothProof zkproof = 5;
310+
bytes zkproof = 5;
316311
bytes spend_authority_signature = 6;
317312
}
318313

@@ -322,7 +317,7 @@ message ReceiveDescription {
322317
bytes epk = 3; // for Encryption
323318
bytes c_enc = 4; // Encryption for incoming, decrypt it with ivk
324319
bytes c_out = 5; // Encryption for audit, decrypt it with ovk
325-
GrothProof zkproof = 6;
320+
bytes zkproof = 6;
326321
}
327322

328323
message ShieldedTransferContract {

0 commit comments

Comments
 (0)