From c5889aee6ce437a32b17fdd837586e2583ae2a21 Mon Sep 17 00:00:00 2001 From: Irina Pereiaslavskaia Date: Fri, 18 Oct 2024 16:38:39 +0300 Subject: [PATCH] Adding an error occurrence in case of a problem with the issue of assets and checks in transfer methods. (#1516) * Adding an error in case of a problem with the issue of assets, adding additional checks in transfer methods. * Fix lint issues. * Adding new method SafeInt64ToUint64. * Fix method SafeInt64ToUint64. * Fix method SafeInt64ToUint64. * Fix method SafeInt64ToUint64. * Fix method for issue assets. --------- Co-authored-by: Nikolay Eskov --- itests/utilities/common.go | 7 +++ itests/utilities/issue/issue.go | 4 ++ itests/utilities/transfer/transfer.go | 69 +++++++++++++++++++++++---- 3 files changed, 71 insertions(+), 9 deletions(-) diff --git a/itests/utilities/common.go b/itests/utilities/common.go index afb3477d6..9c7a90a54 100644 --- a/itests/utilities/common.go +++ b/itests/utilities/common.go @@ -278,6 +278,13 @@ func Abs(x int64) int64 { return x } +func SafeInt64ToUint64(x int64) uint64 { + if x < 0 { + panic("negative number") + } + return uint64(x) +} + func SetFromToAccounts(accountNumbers ...int) (int, int, error) { var from, to int switch len(accountNumbers) { diff --git a/itests/utilities/issue/issue.go b/itests/utilities/issue/issue.go index 6b373f899..eae0e7725 100644 --- a/itests/utilities/issue/issue.go +++ b/itests/utilities/issue/issue.go @@ -97,6 +97,10 @@ func IssuedAssetAmount(suite *f.BaseSuite, version byte, scheme proto.Scheme, ac tx := Send(suite, version, scheme, utl.GetAccount(suite, accountNumber).PublicKey, utl.GetAccount(suite, accountNumber).SecretKey, "Asset", "Common Asset for testing", amount, utl.GetCurrentTimestampInMs(), utl.MinIssueFeeWaves, utl.MaxDecimals, true, true, nil) + if tx.WtErr.ErrWtScala != nil || tx.WtErr.ErrWtGo != nil { + suite.FailNowf("failed to issue assets", "asset: %s, ammount: %d, with errors: Scala: %s, Go: %S", + tx.TxID, amount, tx.WtErr.ErrWtScala, tx.WtErr.ErrWtGo) + } return tx.TxID } diff --git a/itests/utilities/transfer/transfer.go b/itests/utilities/transfer/transfer.go index 64846f382..a80fec2e3 100644 --- a/itests/utilities/transfer/transfer.go +++ b/itests/utilities/transfer/transfer.go @@ -140,20 +140,43 @@ func BroadcastTransferTxAndGetBalances[T any](suite *f.BaseSuite, testdata testd } func TransferringFunds(suite *f.BaseSuite, version byte, scheme proto.Scheme, from, to int, - amount uint64) utl.ConsideredTransaction { + amount uint64) { sender := utl.GetAccount(suite, from) recipient := utl.GetAccount(suite, to) + + // Balances before transferring + senderBalanceGoBefore, senderBalanceScalaBefore := utl.GetAvailableBalanceInWaves(suite, sender.Address) + require.Equal(suite.T(), senderBalanceGoBefore, senderBalanceScalaBefore) + recipientBalanceGoBefore, recipientBalanceScalaBefore := utl.GetAvailableBalanceInWaves(suite, recipient.Address) + require.Equal(suite.T(), recipientBalanceGoBefore, recipientBalanceScalaBefore) + tx := Send(suite, version, scheme, sender.PublicKey, sender.SecretKey, proto.NewOptionalAssetWaves(), proto.NewOptionalAssetWaves(), utl.GetCurrentTimestampInMs(), amount, utl.MinTxFeeWaves, proto.NewRecipientFromAddress(recipient.Address), nil, true) - return tx + require.NoError(suite.T(), tx.WtErr.ErrWtGo, "Reached deadline of Transfer tx in Go") + require.NoError(suite.T(), tx.WtErr.ErrWtScala, "Reached deadline of Transfer tx in Scala") + // Waiting for changing waves balance + err := clients.Retry(utl.DefaultTimeInterval, func() error { + // Balances after transferring + senderBalanceGoAfter, senderBalanceScalaAfter := utl.GetAvailableBalanceInWaves(suite, sender.Address) + require.Equal(suite.T(), senderBalanceGoAfter, senderBalanceScalaAfter) + recipientBalanceGoAfter, recipientBalanceScalaAfter := utl.GetAvailableBalanceInWaves(suite, recipient.Address) + require.Equal(suite.T(), recipientBalanceGoAfter, recipientBalanceScalaAfter) + + if utl.SafeInt64ToUint64(utl.Abs(senderBalanceGoBefore-senderBalanceGoAfter)) != amount && + utl.SafeInt64ToUint64(utl.Abs(recipientBalanceGoAfter-recipientBalanceGoBefore)) != amount { + return errors.New("accounts Waves balances are mismatch") + } + + return nil + }) + require.NoError(suite.T(), err) } func GetNewAccountWithFunds(suite *f.BaseSuite, version byte, scheme proto.Scheme, from int, amount uint64) int { accNumber, _ := utl.AddNewAccount(suite, scheme) - tx := TransferringFunds(suite, version, scheme, from, accNumber, amount) - require.NoError(suite.T(), tx.WtErr.ErrWtGo, "Reached deadline of Transfer tx in Go") - require.NoError(suite.T(), tx.WtErr.ErrWtScala, "Reached deadline of Transfer tx in Scala") + TransferringFunds(suite, version, scheme, from, accNumber, amount) + // Waiting for changing waves balance. err := clients.Retry(utl.DefaultTimeInterval, func() error { balanceGo, balanceScala := utl.GetAvailableBalanceInWaves(suite, utl.GetAccount(suite, accNumber).Address) @@ -171,18 +194,46 @@ func GetNewAccountWithFunds(suite *f.BaseSuite, version byte, scheme proto.Schem func TransferringAssetAmount(suite *f.BaseSuite, version byte, scheme proto.Scheme, assetID crypto.Digest, from, to int, assetAmount ...uint64) { var amount, currentAmount uint64 - currentAmount = uint64(utl.GetAssetBalanceGo(suite, utl.GetAccount(suite, from).Address, assetID)) + sender := utl.GetAccount(suite, from) + recipient := utl.GetAccount(suite, to) + + senderAssetBalanceGoBefore, senderAssetBalanceScalaBefore := utl.GetAssetBalance(suite, sender.Address, assetID) + require.Equal(suite.T(), senderAssetBalanceGoBefore, senderAssetBalanceScalaBefore) + recipientAssetBalanceGoBefore, recipientAssetBalanceScalaBefore := utl.GetAssetBalance( + suite, recipient.Address, assetID) + require.Equal(suite.T(), recipientAssetBalanceGoBefore, recipientAssetBalanceScalaBefore) + + currentAmount = utl.SafeInt64ToUint64(senderAssetBalanceGoBefore) if len(assetAmount) == 1 && assetAmount[0] <= currentAmount { amount = assetAmount[0] } else { amount = currentAmount } - tx := Send(suite, version, scheme, utl.GetAccount(suite, from).PublicKey, - utl.GetAccount(suite, from).SecretKey, *proto.NewOptionalAssetFromDigest(assetID), + + tx := Send(suite, version, scheme, sender.PublicKey, + sender.SecretKey, *proto.NewOptionalAssetFromDigest(assetID), proto.NewOptionalAssetWaves(), utl.GetCurrentTimestampInMs(), amount, utl.MinTxFeeWaves, - proto.NewRecipientFromAddress(utl.GetAccount(suite, to).Address), nil, true) + proto.NewRecipientFromAddress(recipient.Address), nil, true) require.NoError(suite.T(), tx.WtErr.ErrWtGo, "Reached deadline of Transfer tx in Go") require.NoError(suite.T(), tx.WtErr.ErrWtScala, "Reached deadline of Transfer tx in Scala") + // Waiting for changing waves balance + err := clients.Retry(utl.DefaultTimeInterval, func() error { + // Balances after transferring + senderAssetBalanceGoAfter, senderAssetBalanceScalaAfter := utl.GetAssetBalance(suite, sender.Address, assetID) + require.Equal(suite.T(), senderAssetBalanceGoAfter, senderAssetBalanceScalaAfter) + recipientAssetBalanceGoAfter, recipientAssetBalanceScalaAfter := utl.GetAssetBalance( + suite, recipient.Address, assetID) + require.Equal(suite.T(), recipientAssetBalanceGoAfter, recipientAssetBalanceScalaAfter) + + if utl.SafeInt64ToUint64(utl.Abs(senderAssetBalanceGoBefore-senderAssetBalanceGoAfter)) != amount && + utl.SafeInt64ToUint64(utl.Abs(recipientAssetBalanceGoAfter-recipientAssetBalanceGoBefore)) != amount { + return errors.New("accounts asset balances are mismatch") + } + + return nil + }) + require.NoError(suite.T(), err) + } func GetVersions(suite *f.BaseSuite) []byte {