@@ -775,45 +775,94 @@ func TestClient_GenerateMessageHash(t *testing.T) {
775775 assert .NoError (t , err )
776776 assert .Equal (t , expectedHash , hash )
777777 })
778- t .Run ("should return correct hash for batch with deposits with different tokens " , func (t * testing.T ) {
778+ t .Run ("should return same hash for batches with same deposits in different order " , func (t * testing.T ) {
779779 args := createMockSuiClientArgs ()
780780 c , _ := NewSuiClient (args )
781781
782- batch := & batchProcessor.ArgListsBatch {
782+ batchID := uint64 (456 )
783+ batch1 := & batchProcessor.ArgListsBatch {
783784 PeerTokens : [][]byte {[]byte ("token1" ), []byte ("token2" )},
784785 Recipients : [][]byte {[]byte ("recipient1" ), []byte ("recipient2" )},
785786 MvxTokenBytes : [][]byte {[]byte ("mvxToken1" ), []byte ("mvxToken2" )},
786787 Amounts : []* big.Int {big .NewInt (900 ), big .NewInt (560 )},
787788 Nonces : []* big.Int {big .NewInt (640 ), big .NewInt (310 )},
788789 Direction : batchProcessor .FromMultiversX ,
789790 }
790- batchID := uint64 (456 )
791+ batch2 := & batchProcessor.ArgListsBatch {
792+ PeerTokens : [][]byte {[]byte ("token2" ), []byte ("token1" )},
793+ Recipients : [][]byte {[]byte ("recipient2" ), []byte ("recipient1" )},
794+ MvxTokenBytes : [][]byte {[]byte ("mvxToken2" ), []byte ("mvxToken1" )},
795+ Amounts : []* big.Int {big .NewInt (560 ), big .NewInt (900 )},
796+ Nonces : []* big.Int {big .NewInt (310 ), big .NewInt (640 )},
797+ Direction : batchProcessor .FromMultiversX ,
798+ }
791799
792- suiAddress1 := suiAddressFromBytes (batch .Recipients [0 ])
800+ hash1 , err := c .GenerateMessageHash (batch1 , batchID )
801+ assert .NoError (t , err )
802+ assert .NotNil (t , hash1 )
803+
804+ hash2 , err := c .GenerateMessageHash (batch2 , batchID )
805+ assert .NoError (t , err )
806+ assert .NotNil (t , hash2 )
807+
808+ assert .True (t , bytes .Equal (hash1 , hash2 ))
809+
810+ suiAddress1 := suiAddressFromBytes (batch1 .Recipients [0 ])
793811 suiAddressBytes1 , _ := transaction .ConvertSuiAddressStringToBytes (models .SuiAddress (suiAddress1 ))
794812
795- suiAddress2 := suiAddressFromBytes (batch .Recipients [1 ])
813+ suiAddress2 := suiAddressFromBytes (batch1 .Recipients [1 ])
796814 suiAddressBytes2 , _ := transaction .ConvertSuiAddressStringToBytes (models .SuiAddress (suiAddress2 ))
797815
816+ // we use batch1 data to generate expected hashes because is ordered correctly
798817 expectedHashForToken1 := generateHashForTokenGroup (t , batchID , & TokenTransferGroup {
799- Tokens : [][]byte {batch .PeerTokens [0 ]},
818+ Tokens : [][]byte {batch1 .PeerTokens [0 ]},
800819 Recipients : []models.SuiAddressBytes {* suiAddressBytes1 },
801- Amounts : []uint64 {batch .Amounts [0 ].Uint64 ()},
802- Nonces : []uint64 {batch .Nonces [0 ].Uint64 ()},
820+ Amounts : []uint64 {batch1 .Amounts [0 ].Uint64 ()},
821+ Nonces : []uint64 {batch1 .Nonces [0 ].Uint64 ()},
803822 })
804823
805824 expectedHashForToken2 := generateHashForTokenGroup (t , batchID , & TokenTransferGroup {
806- Tokens : [][]byte {batch .PeerTokens [1 ]},
825+ Tokens : [][]byte {batch1 .PeerTokens [1 ]},
807826 Recipients : []models.SuiAddressBytes {* suiAddressBytes2 },
808- Amounts : []uint64 {batch .Amounts [1 ].Uint64 ()},
809- Nonces : []uint64 {batch .Nonces [1 ].Uint64 ()},
827+ Amounts : []uint64 {batch1 .Amounts [1 ].Uint64 ()},
828+ Nonces : []uint64 {batch1 .Nonces [1 ].Uint64 ()},
810829 })
811830
812- hash , err := c .GenerateMessageHash (batch , batchID )
813- assert .NoError (t , err )
814- assert .True (t , bytes .Contains (hash , expectedHashForToken1 ))
815- assert .True (t , bytes .Contains (hash , expectedHashForToken2 ))
816- assert .Equal (t , len (expectedHashForToken1 )+ len (expectedHashForToken2 ), len (hash ))
831+ assert .True (t , bytes .Equal (expectedHashForToken1 , hash1 [:32 ]))
832+ assert .True (t , bytes .Equal (expectedHashForToken2 , hash1 [32 :]))
833+ })
834+ t .Run ("should be deterministic over multiple invocations" , func (t * testing.T ) {
835+ args := createMockSuiClientArgs ()
836+ c , _ := NewSuiClient (args )
837+
838+ batch := & batchProcessor.ArgListsBatch {
839+ PeerTokens : [][]byte {[]byte ("token1" ), []byte ("token2" )},
840+ Recipients : [][]byte {[]byte ("recipient1" ), []byte ("recipient2" )},
841+ MvxTokenBytes : [][]byte {[]byte ("mvxToken1" ), []byte ("mvxToken2" )},
842+ Amounts : []* big.Int {big .NewInt (900 ), big .NewInt (560 )},
843+ Nonces : []* big.Int {big .NewInt (640 ), big .NewInt (310 )},
844+ Direction : batchProcessor .FromMultiversX ,
845+ }
846+ batchID := uint64 (456 )
847+
848+ const iterations = 100
849+ var hashes [][]byte
850+
851+ for i := 0 ; i < iterations ; i ++ {
852+ hash , err := c .GenerateMessageHash (batch , batchID )
853+ assert .NoError (t , err , "iteration %d failed" , i )
854+ assert .NotNil (t , hash )
855+ assert .NotEmpty (t , hash )
856+
857+ hashes = append (hashes , hash )
858+ }
859+
860+ referenceHash := hashes [0 ]
861+ for i := 1 ; i < len (hashes ); i ++ {
862+ assert .True (t , bytes .Equal (referenceHash , hashes [i ]),
863+ "Hash mismatch at iteration %d.\n Expected: %x\n Got: %x" ,
864+ i , referenceHash , hashes [i ])
865+ }
817866 })
818867}
819868
0 commit comments