55import com .google .protobuf .InvalidProtocolBufferException ;
66import com .sun .jna .Pointer ;
77import java .util .Arrays ;
8+ import java .util .HashSet ;
89import java .util .List ;
910import lombok .extern .slf4j .Slf4j ;
1011import 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 }
0 commit comments