github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/token_pause_transaction_unit_test.go (about)

     1  //go:build all || unit
     2  // +build all unit
     3  
     4  package hedera
     5  
     6  /*-
     7   *
     8   * Hedera Go SDK
     9   *
    10   * Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC
    11   *
    12   * Licensed under the Apache License, Version 2.0 (the "License");
    13   * you may not use this file except in compliance with the License.
    14   * You may obtain a copy of the License at
    15   *
    16   *      http://www.apache.org/licenses/LICENSE-2.0
    17   *
    18   * Unless required by applicable law or agreed to in writing, software
    19   * distributed under the License is distributed on an "AS IS" BASIS,
    20   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    21   * See the License for the specific language governing permissions and
    22   * limitations under the License.
    23   *
    24   */
    25  
    26  import (
    27  	"testing"
    28  	"time"
    29  
    30  	"github.com/hashgraph/hedera-protobufs-go/services"
    31  	protobuf "google.golang.org/protobuf/proto"
    32  
    33  	"github.com/stretchr/testify/assert"
    34  	"github.com/stretchr/testify/require"
    35  )
    36  
    37  func TestUnitTokenPause(t *testing.T) {
    38  	t.Parallel()
    39  
    40  	accountID, err := AccountIDFromString("0.0.5005")
    41  	require.NoError(t, err)
    42  	tokenID, err := TokenIDFromString("0.0.5005")
    43  	require.NoError(t, err)
    44  
    45  	tx, err := NewTokenPauseTransaction().
    46  		SetNodeAccountIDs([]AccountID{accountID}).
    47  		SetTransactionID(TransactionIDGenerate(accountID)).
    48  		SetTokenID(tokenID).
    49  		Freeze()
    50  	require.NoError(t, err)
    51  
    52  	pb := tx.build()
    53  	require.Equal(t, pb.GetTokenPause().GetToken().String(), tokenID._ToProtobuf().String())
    54  }
    55  
    56  func TestUnitTokenUnpause(t *testing.T) {
    57  	t.Parallel()
    58  
    59  	accountID, err := AccountIDFromString("0.0.5005")
    60  	require.NoError(t, err)
    61  	tokenID, err := TokenIDFromString("0.0.5005")
    62  	require.NoError(t, err)
    63  
    64  	tx, err := NewTokenUnpauseTransaction().
    65  		SetNodeAccountIDs([]AccountID{accountID}).
    66  		SetTransactionID(TransactionIDGenerate(accountID)).
    67  		SetTokenID(tokenID).
    68  		Freeze()
    69  	require.NoError(t, err)
    70  
    71  	pb := tx.build()
    72  	require.Equal(t, pb.GetTokenUnpause().GetToken().String(), tokenID._ToProtobuf().String())
    73  }
    74  func TestUnitTokenPauseSchedule(t *testing.T) {
    75  	accountID, err := AccountIDFromString("0.0.5005")
    76  	require.NoError(t, err)
    77  	tokenID, err := TokenIDFromString("0.0.5005")
    78  	require.NoError(t, err)
    79  
    80  	tx, err := NewTokenPauseTransaction().
    81  		SetNodeAccountIDs([]AccountID{accountID}).
    82  		SetTransactionID(TransactionIDGenerate(accountID)).
    83  		SetTokenID(tokenID).
    84  		Schedule()
    85  	require.NoError(t, err)
    86  
    87  	scheduled := tx.schedulableBody.GetTokenPause()
    88  	require.Equal(t, scheduled.Token.String(), tokenID._ToProtobuf().String())
    89  }
    90  
    91  func TestUnitTokenPauseTransactionGet(t *testing.T) {
    92  	t.Parallel()
    93  
    94  	tokenID := TokenID{Token: 7}
    95  
    96  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
    97  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
    98  
    99  	transaction, err := NewTokenPauseTransaction().
   100  		SetTransactionID(transactionID).
   101  		SetNodeAccountIDs(nodeAccountID).
   102  		SetTokenID(tokenID).
   103  		SetMaxTransactionFee(NewHbar(10)).
   104  		SetTransactionMemo("").
   105  		SetTransactionValidDuration(60 * time.Second).
   106  		SetRegenerateTransactionID(false).
   107  		Freeze()
   108  	require.NoError(t, err)
   109  
   110  	transaction.GetTransactionID()
   111  	transaction.GetNodeAccountIDs()
   112  
   113  	_, err = transaction.GetTransactionHash()
   114  	require.NoError(t, err)
   115  
   116  	transaction.GetTokenID()
   117  	transaction.GetMaxTransactionFee()
   118  	transaction.GetTransactionMemo()
   119  	transaction.GetRegenerateTransactionID()
   120  	_, err = transaction.GetSignatures()
   121  	require.NoError(t, err)
   122  	transaction.GetRegenerateTransactionID()
   123  	transaction.GetMaxTransactionFee()
   124  	transaction.GetRegenerateTransactionID()
   125  }
   126  
   127  func TestUnitTokenPauseTransactionNothingSet(t *testing.T) {
   128  	t.Parallel()
   129  
   130  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
   131  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   132  
   133  	transaction, err := NewTokenPauseTransaction().
   134  		SetTransactionID(transactionID).
   135  		SetNodeAccountIDs(nodeAccountID).
   136  		Freeze()
   137  	require.NoError(t, err)
   138  
   139  	transaction.GetTransactionID()
   140  	transaction.GetNodeAccountIDs()
   141  
   142  	_, err = transaction.GetTransactionHash()
   143  	require.NoError(t, err)
   144  
   145  	transaction.GetTokenID()
   146  	transaction.GetMaxTransactionFee()
   147  	transaction.GetTransactionMemo()
   148  	transaction.GetRegenerateTransactionID()
   149  	_, err = transaction.GetSignatures()
   150  	require.NoError(t, err)
   151  	transaction.GetRegenerateTransactionID()
   152  	transaction.GetMaxTransactionFee()
   153  	transaction.GetRegenerateTransactionID()
   154  }
   155  
   156  func TestUnitTokenUnpauseTransactionGet(t *testing.T) {
   157  	t.Parallel()
   158  
   159  	tokenID := TokenID{Token: 7}
   160  
   161  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
   162  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   163  
   164  	transaction, err := NewTokenUnpauseTransaction().
   165  		SetTransactionID(transactionID).
   166  		SetNodeAccountIDs(nodeAccountID).
   167  		SetTokenID(tokenID).
   168  		SetMaxTransactionFee(NewHbar(10)).
   169  		SetTransactionMemo("").
   170  		SetTransactionValidDuration(60 * time.Second).
   171  		SetRegenerateTransactionID(false).
   172  		Freeze()
   173  	require.NoError(t, err)
   174  
   175  	transaction.GetTransactionID()
   176  	transaction.GetNodeAccountIDs()
   177  
   178  	_, err = transaction.GetTransactionHash()
   179  	require.NoError(t, err)
   180  
   181  	transaction.GetTokenID()
   182  	transaction.GetMaxTransactionFee()
   183  	transaction.GetTransactionMemo()
   184  	transaction.GetRegenerateTransactionID()
   185  	_, err = transaction.GetSignatures()
   186  	require.NoError(t, err)
   187  	transaction.GetRegenerateTransactionID()
   188  	transaction.GetMaxTransactionFee()
   189  	transaction.GetRegenerateTransactionID()
   190  }
   191  
   192  func TestUnitTokenUnpauseTransactionNothingSet(t *testing.T) {
   193  	t.Parallel()
   194  
   195  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
   196  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   197  
   198  	transaction, err := NewTokenUnpauseTransaction().
   199  		SetTransactionID(transactionID).
   200  		SetNodeAccountIDs(nodeAccountID).
   201  		Freeze()
   202  	require.NoError(t, err)
   203  
   204  	transaction.GetTransactionID()
   205  	transaction.GetNodeAccountIDs()
   206  
   207  	_, err = transaction.GetTransactionHash()
   208  	require.NoError(t, err)
   209  
   210  	transaction.GetTokenID()
   211  	transaction.GetMaxTransactionFee()
   212  	transaction.GetTransactionMemo()
   213  	transaction.GetRegenerateTransactionID()
   214  	_, err = transaction.GetSignatures()
   215  	require.NoError(t, err)
   216  	transaction.GetRegenerateTransactionID()
   217  	transaction.GetMaxTransactionFee()
   218  	transaction.GetRegenerateTransactionID()
   219  }
   220  
   221  func TestUnitTokenUnpauseTransactionCoverage(t *testing.T) {
   222  	t.Parallel()
   223  
   224  	checksum := "dmqui"
   225  	grpc := time.Second * 30
   226  	token := TokenID{Token: 3, checksum: &checksum}
   227  	nodeAccountID := []AccountID{{Account: 10}}
   228  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   229  
   230  	newKey, err := PrivateKeyGenerateEd25519()
   231  	require.NoError(t, err)
   232  
   233  	client, err := _NewMockClient()
   234  	client.SetLedgerID(*NewLedgerIDTestnet())
   235  	require.NoError(t, err)
   236  	client.SetAutoValidateChecksums(true)
   237  
   238  	transaction, err := NewTokenUnpauseTransaction().
   239  		SetTransactionID(transactionID).
   240  		SetNodeAccountIDs(nodeAccountID).
   241  		SetTokenID(token).
   242  		SetGrpcDeadline(&grpc).
   243  		SetMaxTransactionFee(NewHbar(3)).
   244  		SetMaxRetry(3).
   245  		SetMaxBackoff(time.Second * 30).
   246  		SetMinBackoff(time.Second * 10).
   247  		SetTransactionMemo("no").
   248  		SetTransactionValidDuration(time.Second * 30).
   249  		SetRegenerateTransactionID(false).
   250  		Freeze()
   251  	require.NoError(t, err)
   252  
   253  	err = transaction.validateNetworkOnIDs(client)
   254  	require.NoError(t, err)
   255  	_, err = transaction.Schedule()
   256  	require.NoError(t, err)
   257  	transaction.GetTransactionID()
   258  	transaction.GetNodeAccountIDs()
   259  	transaction.GetMaxRetry()
   260  	transaction.GetMaxTransactionFee()
   261  	transaction.GetMaxBackoff()
   262  	transaction.GetMinBackoff()
   263  	transaction.GetRegenerateTransactionID()
   264  	byt, err := transaction.ToBytes()
   265  	require.NoError(t, err)
   266  	txFromBytes, err := TransactionFromBytes(byt)
   267  	require.NoError(t, err)
   268  	sig, err := newKey.SignTransaction(&transaction.Transaction)
   269  	require.NoError(t, err)
   270  
   271  	_, err = transaction.GetTransactionHash()
   272  	require.NoError(t, err)
   273  	transaction.GetMaxTransactionFee()
   274  	transaction.GetTransactionMemo()
   275  	transaction.GetRegenerateTransactionID()
   276  	transaction.GetTokenID()
   277  	_, err = transaction.GetSignatures()
   278  	require.NoError(t, err)
   279  	transaction.getName()
   280  	switch b := txFromBytes.(type) {
   281  	case TokenUnpauseTransaction:
   282  		b.AddSignature(newKey.PublicKey(), sig)
   283  	}
   284  }
   285  
   286  func TestUnitTokenUnpauseTransactionMock(t *testing.T) {
   287  	t.Parallel()
   288  
   289  	newKey, err := PrivateKeyFromStringEd25519("302e020100300506032b657004220420a869f4c6191b9c8c99933e7f6b6611711737e4b1a1a5a4cb5370e719a1f6df98")
   290  	require.NoError(t, err)
   291  
   292  	call := func(request *services.Transaction) *services.TransactionResponse {
   293  		require.NotEmpty(t, request.SignedTransactionBytes)
   294  		signedTransaction := services.SignedTransaction{}
   295  		_ = protobuf.Unmarshal(request.SignedTransactionBytes, &signedTransaction)
   296  
   297  		require.NotEmpty(t, signedTransaction.BodyBytes)
   298  		transactionBody := services.TransactionBody{}
   299  		_ = protobuf.Unmarshal(signedTransaction.BodyBytes, &transactionBody)
   300  
   301  		require.NotNil(t, transactionBody.TransactionID)
   302  		transactionId := transactionBody.TransactionID.String()
   303  		require.NotEqual(t, "", transactionId)
   304  
   305  		sigMap := signedTransaction.GetSigMap()
   306  		require.NotNil(t, sigMap)
   307  
   308  		return &services.TransactionResponse{
   309  			NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK,
   310  		}
   311  	}
   312  	responses := [][]interface{}{{
   313  		call,
   314  	}}
   315  
   316  	client, server := NewMockClientAndServer(responses)
   317  	defer server.Close()
   318  
   319  	checksum := "dmqui"
   320  	token := TokenID{Token: 3, checksum: &checksum}
   321  
   322  	freez, err := NewTokenUnpauseTransaction().
   323  		SetNodeAccountIDs([]AccountID{{Account: 3}}).
   324  		SetTokenID(token).
   325  		FreezeWith(client)
   326  	require.NoError(t, err)
   327  
   328  	_, err = freez.Sign(newKey).Execute(client)
   329  	require.NoError(t, err)
   330  }
   331  
   332  func TestUnitTokenPauseTransaction_SetMaxRetry(t *testing.T) {
   333  	t.Parallel()
   334  	transaction := NewTokenPauseTransaction()
   335  	transaction.SetMaxRetry(5)
   336  
   337  	require.Equal(t, 5, transaction.GetMaxRetry())
   338  }
   339  
   340  func TestUnitTokenPauseTransaction_AddSignature(t *testing.T) {
   341  	t.Parallel()
   342  	client, _ := ClientFromConfig([]byte(testClientJSONWithoutMirrorNetwork))
   343  
   344  	nodeAccountId, err := AccountIDFromString("0.0.3")
   345  	require.NoError(t, err)
   346  
   347  	nodeIdList := []AccountID{nodeAccountId}
   348  
   349  	transaction, err := NewTokenPauseTransaction().
   350  		SetNodeAccountIDs(nodeIdList).
   351  		FreezeWith(client)
   352  
   353  	privateKey, _ := PrivateKeyGenerateEd25519()
   354  
   355  	signature, err := privateKey.SignTransaction(&transaction.Transaction)
   356  	require.NoError(t, err)
   357  
   358  	signs, err := transaction.GetSignatures()
   359  	for key := range signs[nodeAccountId] {
   360  		require.Equal(t, signs[nodeAccountId][key], signature)
   361  	}
   362  
   363  	require.NoError(t, err)
   364  
   365  	privateKey2, _ := PrivateKeyGenerateEd25519()
   366  	publicKey2 := privateKey2.PublicKey()
   367  
   368  	signedTransaction := transaction.AddSignature(publicKey2, signature)
   369  	signs2, err := signedTransaction.GetSignatures()
   370  	require.NoError(t, err)
   371  
   372  	for key := range signs2[nodeAccountId] {
   373  		require.Equal(t, signs2[nodeAccountId][key], signature)
   374  	}
   375  }
   376  
   377  func TestUnitTokenPauseTransaction_SignWithOperator(t *testing.T) {
   378  	t.Parallel()
   379  	client, _ := ClientFromConfig([]byte(testClientJSONWithoutMirrorNetwork))
   380  	privateKey, _ := PrivateKeyGenerateEd25519()
   381  	publicKey := privateKey.PublicKey()
   382  	operatorId, _ := AccountIDFromString("0.0.10")
   383  
   384  	client.SetOperator(operatorId, privateKey)
   385  
   386  	nodeAccountId, err := AccountIDFromString("0.0.3")
   387  	require.NoError(t, err)
   388  	nodeIdList := []AccountID{nodeAccountId}
   389  
   390  	transaction, err := NewTokenPauseTransaction().
   391  		SetNodeAccountIDs(nodeIdList).
   392  		SetTokenID(TokenID{Token: 3}).
   393  		SignWithOperator(client)
   394  
   395  	require.NoError(t, err)
   396  	require.NotNil(t, transaction)
   397  
   398  	privateKey2, _ := PrivateKeyGenerateEd25519()
   399  	publicKey2 := privateKey2.PublicKey()
   400  	client.SetOperator(operatorId, privateKey2)
   401  
   402  	transactionSignedWithOp, err := transaction.SignWithOperator(client)
   403  	require.NoError(t, err)
   404  	require.NotNil(t, transactionSignedWithOp)
   405  
   406  	assert.Contains(t, transactionSignedWithOp.Transaction.publicKeys, publicKey)
   407  	assert.Contains(t, transactionSignedWithOp.Transaction.publicKeys, publicKey2)
   408  
   409  	// test errors
   410  	client.operator = nil
   411  	tx, err := NewTokenPauseTransaction().
   412  		SetNodeAccountIDs(nodeIdList).
   413  		SetTokenID(TokenID{Token: 3}).
   414  		SignWithOperator(client)
   415  
   416  	require.Error(t, err)
   417  	require.Nil(t, tx)
   418  
   419  	client = nil
   420  	tx, err = NewTokenPauseTransaction().
   421  		SetNodeAccountIDs(nodeIdList).
   422  		SetTokenID(TokenID{Token: 3}).
   423  		SignWithOperator(client)
   424  
   425  	require.Error(t, err)
   426  	require.Nil(t, tx)
   427  }
   428  
   429  func TestUnitTokenPauseTransaction_SetMaxBackoff(t *testing.T) {
   430  	t.Parallel()
   431  	transaction := NewTokenPauseTransaction()
   432  	maxBackoff := 10 * time.Second
   433  
   434  	transaction.SetMaxBackoff(maxBackoff)
   435  
   436  	require.Equal(t, maxBackoff, transaction.GetMaxBackoff())
   437  
   438  	// test max.Nanoseconds() < 0
   439  	transaction2 := NewTokenPauseTransaction()
   440  	maxBackoff2 := -1 * time.Second
   441  
   442  	require.Panics(t, func() { transaction2.SetMaxBackoff(maxBackoff2) })
   443  
   444  	// test max.Nanoseconds() < min.Nanoseconds()
   445  	transaction3 := NewTokenPauseTransaction()
   446  	maxBackoff3 := 1 * time.Second
   447  	minBackoff3 := 2 * time.Second
   448  
   449  	transaction3.SetMinBackoff(minBackoff3)
   450  
   451  	require.Panics(t, func() { transaction3.SetMaxBackoff(maxBackoff3) })
   452  }
   453  
   454  func TestUnitTokenPauseTransaction_GetMaxBackoff(t *testing.T) {
   455  	t.Parallel()
   456  	transaction := NewTokenPauseTransaction()
   457  
   458  	require.Equal(t, 8*time.Second, transaction.GetMaxBackoff())
   459  }
   460  
   461  func TestUnitTokenPauseTransaction_SetMinBackoff(t *testing.T) {
   462  	t.Parallel()
   463  	transaction := NewTokenPauseTransaction()
   464  	minBackoff := 1 * time.Second
   465  
   466  	transaction.SetMinBackoff(minBackoff)
   467  
   468  	require.Equal(t, minBackoff, transaction.GetMinBackoff())
   469  
   470  	// test min.Nanoseconds() < 0
   471  	transaction2 := NewTokenPauseTransaction()
   472  	minBackoff2 := -1 * time.Second
   473  
   474  	require.Panics(t, func() { transaction2.SetMinBackoff(minBackoff2) })
   475  
   476  	// test transaction.maxBackoff.Nanoseconds() < min.Nanoseconds()
   477  	transaction3 := NewTokenPauseTransaction()
   478  	minBackoff3 := 10 * time.Second
   479  
   480  	require.Panics(t, func() { transaction3.SetMinBackoff(minBackoff3) })
   481  }
   482  
   483  func TestUnitTokenPauseTransaction_GetMinBackoff(t *testing.T) {
   484  	t.Parallel()
   485  	transaction := NewTokenPauseTransaction()
   486  
   487  	require.Equal(t, 250*time.Millisecond, transaction.GetMinBackoff())
   488  }
   489  
   490  func TestUnitTokenPauseTransaction_SetLogLevel(t *testing.T) {
   491  	t.Parallel()
   492  	transaction := NewTokenPauseTransaction()
   493  
   494  	transaction.SetLogLevel(LoggerLevelDebug)
   495  
   496  	level := transaction.GetLogLevel()
   497  	require.Equal(t, LoggerLevelDebug, *level)
   498  }