@@ -434,6 +434,206 @@ var _ bool = Describe("Token EndToEnd", func() {
434
434
Expect (* ordererStatus ).To (Equal (common .Status_SUCCESS ))
435
435
Expect (committed ).To (BeFalse ())
436
436
})
437
+
438
+ It ("User1's spending its token to a nil output, to zero value" , func () {
439
+ By ("User1 issuing tokens to User2" )
440
+ tClient := GetTokenClient (network , peer , orderer , "User1" , "Org1MSP" )
441
+ txID := RunIssueRequest (tClient , tokensToIssue , expectedTokenTransaction )
442
+
443
+ By ("User2 lists tokens as sanity check" )
444
+ tClient = GetTokenClient (network , peer , orderer , "User2" , "Org1MSP" )
445
+ issuedTokens = RunListTokens (tClient , expectedUnspentTokens )
446
+ Expect (issuedTokens ).ToNot (BeNil ())
447
+ Expect (len (issuedTokens )).To (Equal (1 ))
448
+
449
+ By ("User2 attempts to spend to a nil recipient by using the prover peer" )
450
+ tempTxID , ordererStatus , committed , err := RunTransferRequestWithFailure (tClient , issuedTokens , nil )
451
+ Expect (err ).To (HaveOccurred ())
452
+ Expect (err ).To (MatchError ("error from prover: invalid recipient in transfer request 'identity cannot be nil'" ))
453
+ Expect (ordererStatus ).To (BeNil ())
454
+ Expect (committed ).To (BeFalse ())
455
+
456
+ By ("Check that nothing changed" )
457
+ tClient = GetTokenClient (network , peer , orderer , "User2" , "Org1MSP" )
458
+ issuedTokens = RunListTokens (tClient , expectedUnspentTokens )
459
+ Expect (issuedTokens ).ToNot (BeNil ())
460
+ Expect (len (issuedTokens )).To (Equal (1 ))
461
+
462
+ By ("User2 attempts to spend to a nil recipient by assembling a faulty token transaction" )
463
+ expectedTokenTransaction = & token.TokenTransaction {
464
+ Action : & token.TokenTransaction_TokenAction {
465
+ TokenAction : & token.TokenAction {
466
+ Data : & token.TokenAction_Transfer {
467
+ Transfer : & token.Transfer {
468
+ Inputs : []* token.TokenId {{TxId : txID , Index : 0 }},
469
+ Outputs : []* token.Token {{
470
+ Owner : nil ,
471
+ Type : "ABC123" ,
472
+ Quantity : ToHex (119 ),
473
+ }}}}}}}
474
+ tempTxID , ordererStatus , committed , err = SubmitTokenTx (tClient , expectedTokenTransaction )
475
+ Expect (err ).To (HaveOccurred ())
476
+ Expect (err ).To (MatchError (fmt .Sprintf ("transaction [%s] status is not valid: INVALID_OTHER_REASON" , tempTxID )))
477
+ Expect (* ordererStatus ).To (Equal (common .Status_SUCCESS ))
478
+ Expect (committed ).To (BeFalse ())
479
+
480
+ By ("Check that nothing changed" )
481
+ tClient = GetTokenClient (network , peer , orderer , "User2" , "Org1MSP" )
482
+ issuedTokens = RunListTokens (tClient , expectedUnspentTokens )
483
+ Expect (issuedTokens ).ToNot (BeNil ())
484
+ Expect (len (issuedTokens )).To (Equal (1 ))
485
+
486
+ By ("User2 attempts to spend to an invalid recipient by using the prover peer" )
487
+ tempTxID , ordererStatus , committed , err = RunTransferRequestWithFailure (tClient , issuedTokens , & token.TokenOwner {Raw : []byte {1 , 2 , 3 , 4 }})
488
+ Expect (err ).To (HaveOccurred ())
489
+ Expect (err ).To (MatchError ("error from prover: invalid recipient in transfer request 'identity [0x7261773a225c3030315c3030325c3030335c3030342220] cannot be deserialised: could not deserialize a SerializedIdentity: proto: msp.SerializedIdentity: illegal tag 0 (wire type 1)'" ))
490
+ Expect (ordererStatus ).To (BeNil ())
491
+ Expect (committed ).To (BeFalse ())
492
+
493
+ By ("Check that nothing changed" )
494
+ tClient = GetTokenClient (network , peer , orderer , "User2" , "Org1MSP" )
495
+ issuedTokens = RunListTokens (tClient , expectedUnspentTokens )
496
+ Expect (issuedTokens ).ToNot (BeNil ())
497
+ Expect (len (issuedTokens )).To (Equal (1 ))
498
+
499
+ By ("User2 attempts to spend to an invalid recipient by assembling a faulty token transaction" )
500
+ expectedTokenTransaction = & token.TokenTransaction {
501
+ Action : & token.TokenTransaction_TokenAction {
502
+ TokenAction : & token.TokenAction {
503
+ Data : & token.TokenAction_Transfer {
504
+ Transfer : & token.Transfer {
505
+ Inputs : []* token.TokenId {{TxId : txID , Index : 0 }},
506
+ Outputs : []* token.Token {{
507
+ Owner : & token.TokenOwner {Raw : []byte {1 , 2 , 3 , 4 }},
508
+ Type : "ABC123" ,
509
+ Quantity : ToHex (119 ),
510
+ }}}}}}}
511
+ tempTxID , ordererStatus , committed , err = SubmitTokenTx (tClient , expectedTokenTransaction )
512
+ Expect (err ).To (HaveOccurred ())
513
+ Expect (err ).To (MatchError (fmt .Sprintf ("transaction [%s] status is not valid: INVALID_OTHER_REASON" , tempTxID )))
514
+ Expect (* ordererStatus ).To (Equal (common .Status_SUCCESS ))
515
+ Expect (committed ).To (BeFalse ())
516
+
517
+ By ("Check that nothing changed" )
518
+ tClient = GetTokenClient (network , peer , orderer , "User2" , "Org1MSP" )
519
+ issuedTokens = RunListTokens (tClient , expectedUnspentTokens )
520
+ Expect (issuedTokens ).ToNot (BeNil ())
521
+ Expect (len (issuedTokens )).To (Equal (1 ))
522
+
523
+ By ("User2 attempts to transfer zero amount by assembling a faulty token transaction by using the prover peer" )
524
+ tempTxID , ordererStatus , committed , err = RunTransferRequestWithSharesAddFailure (tClient , issuedTokens ,
525
+ []* token.RecipientTransferShare {{Recipient : recipientUser1 , Quantity : ToHex (0 )}})
526
+ Expect (err ).To (HaveOccurred ())
527
+ Expect (err ).To (MatchError ("error from prover: invalid quantity in transfer request 'quantity must be larger than 0'" ))
528
+ Expect (ordererStatus ).To (BeNil ())
529
+ Expect (committed ).To (BeFalse ())
530
+
531
+ By ("Check that nothing changed" )
532
+ tClient = GetTokenClient (network , peer , orderer , "User2" , "Org1MSP" )
533
+ issuedTokens = RunListTokens (tClient , expectedUnspentTokens )
534
+ Expect (issuedTokens ).ToNot (BeNil ())
535
+ Expect (len (issuedTokens )).To (Equal (1 ))
536
+
537
+ By ("User2 attempts to transfer zero amount by assembling a faulty token transaction" )
538
+ expectedTokenTransaction = & token.TokenTransaction {
539
+ Action : & token.TokenTransaction_TokenAction {
540
+ TokenAction : & token.TokenAction {
541
+ Data : & token.TokenAction_Transfer {
542
+ Transfer : & token.Transfer {
543
+ Inputs : []* token.TokenId {{TxId : txID , Index : 0 }},
544
+ Outputs : []* token.Token {{
545
+ Owner : recipientUser1 ,
546
+ Type : "ABC123" ,
547
+ Quantity : ToHex (0 ),
548
+ }}}}}}}
549
+ tempTxID , ordererStatus , committed , err = SubmitTokenTx (tClient , expectedTokenTransaction )
550
+ Expect (err ).To (HaveOccurred ())
551
+ Expect (err ).To (MatchError (fmt .Sprintf ("transaction [%s] status is not valid: INVALID_OTHER_REASON" , tempTxID )))
552
+ Expect (* ordererStatus ).To (Equal (common .Status_SUCCESS ))
553
+ Expect (committed ).To (BeFalse ())
554
+
555
+ By ("Check that nothing changed" )
556
+ tClient = GetTokenClient (network , peer , orderer , "User2" , "Org1MSP" )
557
+ issuedTokens = RunListTokens (tClient , expectedUnspentTokens )
558
+ Expect (issuedTokens ).ToNot (BeNil ())
559
+ Expect (len (issuedTokens )).To (Equal (1 ))
560
+
561
+ By ("User2 attempts to transfer an over supported precision amount by assembling a faulty token transaction by using the prover peer" )
562
+ tempTxID , ordererStatus , committed , err = RunTransferRequestWithSharesAddFailure (tClient , issuedTokens ,
563
+ []* token.RecipientTransferShare {{Recipient : recipientUser1 , Quantity : "99999999999999999999" }})
564
+ Expect (err ).To (HaveOccurred ())
565
+ Expect (err ).To (MatchError ("error from prover: invalid quantity in transfer request '99999999999999999999 has precision 67 > 64'" ))
566
+ Expect (ordererStatus ).To (BeNil ())
567
+ Expect (committed ).To (BeFalse ())
568
+
569
+ By ("Check that nothing changed" )
570
+ tClient = GetTokenClient (network , peer , orderer , "User2" , "Org1MSP" )
571
+ issuedTokens = RunListTokens (tClient , expectedUnspentTokens )
572
+ Expect (issuedTokens ).ToNot (BeNil ())
573
+ Expect (len (issuedTokens )).To (Equal (1 ))
574
+
575
+ By ("User2 attempts to transfer an over supported precision amount by assembling a faulty token transaction" )
576
+ expectedTokenTransaction = & token.TokenTransaction {
577
+ Action : & token.TokenTransaction_TokenAction {
578
+ TokenAction : & token.TokenAction {
579
+ Data : & token.TokenAction_Transfer {
580
+ Transfer : & token.Transfer {
581
+ Inputs : []* token.TokenId {{TxId : txID , Index : 0 }},
582
+ Outputs : []* token.Token {{
583
+ Owner : recipientUser1 ,
584
+ Type : "ABC123" ,
585
+ Quantity : "99999999999999999999" ,
586
+ }}}}}}}
587
+ tempTxID , ordererStatus , committed , err = SubmitTokenTx (tClient , expectedTokenTransaction )
588
+ Expect (err ).To (HaveOccurred ())
589
+ Expect (err ).To (MatchError (fmt .Sprintf ("transaction [%s] status is not valid: INVALID_OTHER_REASON" , tempTxID )))
590
+ Expect (* ordererStatus ).To (Equal (common .Status_SUCCESS ))
591
+ Expect (committed ).To (BeFalse ())
592
+
593
+ By ("Check that nothing changed" )
594
+ tClient = GetTokenClient (network , peer , orderer , "User2" , "Org1MSP" )
595
+ issuedTokens = RunListTokens (tClient , expectedUnspentTokens )
596
+ Expect (issuedTokens ).ToNot (BeNil ())
597
+ Expect (len (issuedTokens )).To (Equal (1 ))
598
+
599
+ By ("User2 attempts to transfer a negative amount by assembling a faulty token transaction by using the prover peer" )
600
+ tempTxID , ordererStatus , committed , err = RunTransferRequestWithSharesAddFailure (tClient , issuedTokens ,
601
+ []* token.RecipientTransferShare {{Recipient : recipientUser1 , Quantity : "-99999999999999999999" }})
602
+ Expect (err ).To (HaveOccurred ())
603
+ Expect (err ).To (MatchError ("error from prover: invalid quantity in transfer request 'quantity must be larger than 0'" ))
604
+ Expect (ordererStatus ).To (BeNil ())
605
+ Expect (committed ).To (BeFalse ())
606
+
607
+ By ("Check that nothing changed" )
608
+ tClient = GetTokenClient (network , peer , orderer , "User2" , "Org1MSP" )
609
+ issuedTokens = RunListTokens (tClient , expectedUnspentTokens )
610
+ Expect (issuedTokens ).ToNot (BeNil ())
611
+ Expect (len (issuedTokens )).To (Equal (1 ))
612
+
613
+ By ("User2 attempts to transfer a negative amount by assembling a faulty token transaction" )
614
+ expectedTokenTransaction = & token.TokenTransaction {
615
+ Action : & token.TokenTransaction_TokenAction {
616
+ TokenAction : & token.TokenAction {
617
+ Data : & token.TokenAction_Transfer {
618
+ Transfer : & token.Transfer {
619
+ Inputs : []* token.TokenId {{TxId : txID , Index : 0 }},
620
+ Outputs : []* token.Token {{
621
+ Owner : recipientUser1 ,
622
+ Type : "ABC123" ,
623
+ Quantity : "-99999999999999999999" ,
624
+ }}}}}}}
625
+ tempTxID , ordererStatus , committed , err = SubmitTokenTx (tClient , expectedTokenTransaction )
626
+ Expect (err ).To (HaveOccurred ())
627
+ Expect (err ).To (MatchError (fmt .Sprintf ("transaction [%s] status is not valid: INVALID_OTHER_REASON" , tempTxID )))
628
+ Expect (* ordererStatus ).To (Equal (common .Status_SUCCESS ))
629
+ Expect (committed ).To (BeFalse ())
630
+
631
+ By ("Check that nothing changed" )
632
+ tClient = GetTokenClient (network , peer , orderer , "User2" , "Org1MSP" )
633
+ issuedTokens = RunListTokens (tClient , expectedUnspentTokens )
634
+ Expect (issuedTokens ).ToNot (BeNil ())
635
+ Expect (len (issuedTokens )).To (Equal (1 ))
636
+ })
437
637
})
438
638
439
639
})
@@ -563,6 +763,22 @@ func RunTransferRequestWithFailure(c *tokenclient.Client, inputTokens []*token.T
563
763
return txid , ordererStatus , committed , err
564
764
}
565
765
766
+ func RunTransferRequestWithSharesAddFailure (c * tokenclient.Client , inputTokens []* token.TokenOutput , shares []* token.RecipientTransferShare ) (string , * common.Status , bool , error ) {
767
+ inputTokenIDs := make ([]* token.TokenId , len (inputTokens ))
768
+ sum := plain .NewZeroQuantity (64 )
769
+ var err error
770
+ for i , inToken := range inputTokens {
771
+ inputTokenIDs [i ] = & token.TokenId {TxId : inToken .GetId ().TxId , Index : inToken .GetId ().Index }
772
+
773
+ v , err := plain .ToQuantity (inToken .GetQuantity (), 64 )
774
+ Expect (err ).NotTo (HaveOccurred ())
775
+ sum , err = sum .Add (v )
776
+ Expect (err ).NotTo (HaveOccurred ())
777
+ }
778
+ _ , txid , ordererStatus , committed , err := c .Transfer (inputTokenIDs , shares , 30 * time .Second )
779
+ return txid , ordererStatus , committed , err
780
+ }
781
+
566
782
func SubmitTokenTx (c * tokenclient.Client , tokenTx * token.TokenTransaction ) (string , * common.Status , bool , error ) {
567
783
serializedTokenTx , err := proto .Marshal (tokenTx )
568
784
Expect (err ).ToNot (HaveOccurred ())
0 commit comments