github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/topic_update_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  
    35  	"github.com/stretchr/testify/require"
    36  )
    37  
    38  func TestUnitTopicUpdateTransactionValidate(t *testing.T) {
    39  	t.Parallel()
    40  
    41  	client, err := _NewMockClient()
    42  	client.SetLedgerID(*NewLedgerIDTestnet())
    43  	require.NoError(t, err)
    44  	client.SetAutoValidateChecksums(true)
    45  	accountID, err := AccountIDFromString("0.0.123-esxsf")
    46  	require.NoError(t, err)
    47  	topicID, err := TopicIDFromString("0.0.123-esxsf")
    48  	require.NoError(t, err)
    49  
    50  	topicUpdate := NewTopicUpdateTransaction().
    51  		SetTopicID(topicID).
    52  		SetAutoRenewAccountID(accountID)
    53  
    54  	err = topicUpdate.validateNetworkOnIDs(client)
    55  	require.NoError(t, err)
    56  }
    57  
    58  func TestUnitTopicUpdateTransactionValidateWrong(t *testing.T) {
    59  	t.Parallel()
    60  
    61  	client, err := _NewMockClient()
    62  	client.SetLedgerID(*NewLedgerIDTestnet())
    63  	require.NoError(t, err)
    64  	client.SetAutoValidateChecksums(true)
    65  	accountID, err := AccountIDFromString("0.0.123-rmkykd")
    66  	require.NoError(t, err)
    67  	topicID, err := TopicIDFromString("0.0.123-rmkykd")
    68  	require.NoError(t, err)
    69  
    70  	topicUpdate := NewTopicUpdateTransaction().
    71  		SetTopicID(topicID).
    72  		SetAutoRenewAccountID(accountID)
    73  
    74  	err = topicUpdate.validateNetworkOnIDs(client)
    75  	assert.Error(t, err)
    76  	if err != nil {
    77  		assert.Equal(t, "network mismatch or wrong checksum given, given checksum: rmkykd, correct checksum esxsf, network: testnet", err.Error())
    78  	}
    79  }
    80  
    81  func TestUnitTopicUpdateTransactionGet(t *testing.T) {
    82  	t.Parallel()
    83  
    84  	accountID := AccountID{Account: 3}
    85  
    86  	newKey, err := PrivateKeyGenerateEd25519()
    87  	require.NoError(t, err)
    88  
    89  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
    90  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
    91  
    92  	transaction, err := NewTopicUpdateTransaction().
    93  		SetTransactionID(transactionID).
    94  		SetNodeAccountIDs(nodeAccountID).
    95  		SetAutoRenewAccountID(accountID).
    96  		SetTopicID(TopicID{Topic: 7}).
    97  		SetAdminKey(newKey).
    98  		SetSubmitKey(newKey).
    99  		SetTopicMemo("ad").
   100  		SetAutoRenewPeriod(60 * time.Second).
   101  		SetMaxTransactionFee(NewHbar(10)).
   102  		SetTransactionMemo("").
   103  		SetTransactionValidDuration(60 * time.Second).
   104  		SetRegenerateTransactionID(false).
   105  		Freeze()
   106  	require.NoError(t, err)
   107  
   108  	transaction.GetTransactionID()
   109  	transaction.GetNodeAccountIDs()
   110  
   111  	_, err = transaction.GetTransactionHash()
   112  	require.NoError(t, err)
   113  
   114  	transaction.GetTopicID()
   115  	transaction.GetAutoRenewAccountID()
   116  	transaction.GetAdminKey()
   117  	transaction.GetSubmitKey()
   118  	transaction.GetTopicMemo()
   119  	transaction.GetAutoRenewPeriod()
   120  	transaction.GetMaxTransactionFee()
   121  	transaction.GetTransactionMemo()
   122  	transaction.GetRegenerateTransactionID()
   123  	_, err = transaction.GetSignatures()
   124  	require.NoError(t, err)
   125  	transaction.GetRegenerateTransactionID()
   126  	transaction.GetMaxTransactionFee()
   127  	transaction.GetRegenerateTransactionID()
   128  }
   129  
   130  func TestUnitTopicUpdateTransactionNothingSet(t *testing.T) {
   131  	t.Parallel()
   132  
   133  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
   134  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   135  
   136  	transaction, err := NewTopicUpdateTransaction().
   137  		SetTransactionID(transactionID).
   138  		SetNodeAccountIDs(nodeAccountID).
   139  		Freeze()
   140  	require.NoError(t, err)
   141  
   142  	transaction.GetTransactionID()
   143  	transaction.GetNodeAccountIDs()
   144  
   145  	_, err = transaction.GetTransactionHash()
   146  	require.NoError(t, err)
   147  
   148  	transaction.GetTopicID()
   149  	transaction.GetAutoRenewAccountID()
   150  	transaction.GetAdminKey()
   151  	transaction.GetSubmitKey()
   152  	transaction.GetTopicMemo()
   153  	transaction.GetAutoRenewPeriod()
   154  	transaction.GetMaxTransactionFee()
   155  	transaction.GetTransactionMemo()
   156  	transaction.GetRegenerateTransactionID()
   157  	_, err = transaction.GetSignatures()
   158  	require.NoError(t, err)
   159  	transaction.GetRegenerateTransactionID()
   160  	transaction.GetMaxTransactionFee()
   161  	transaction.GetRegenerateTransactionID()
   162  }
   163  
   164  func TestUnitTopicUpdateTransactionProtoCheck(t *testing.T) {
   165  	t.Parallel()
   166  
   167  	topicID := TopicID{Topic: 5}
   168  	accountID := AccountID{Account: 23}
   169  	nodeAccountID := []AccountID{{Account: 10}, {Account: 11}, {Account: 12}}
   170  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   171  
   172  	newKey, err := PrivateKeyGenerateEd25519()
   173  	require.NoError(t, err)
   174  	newKey2, err := PrivateKeyGenerateEd25519()
   175  	require.NoError(t, err)
   176  
   177  	transaction, err := NewTopicUpdateTransaction().
   178  		SetTransactionID(transactionID).
   179  		SetNodeAccountIDs(nodeAccountID).
   180  		SetTopicID(topicID).
   181  		SetAutoRenewAccountID(accountID).
   182  		SetAdminKey(newKey).
   183  		SetSubmitKey(newKey2).
   184  		SetTopicMemo("memo").
   185  		SetAutoRenewPeriod(time.Second * 3).
   186  		SetExpirationTime(time.Unix(34, 12)).
   187  		Freeze()
   188  	require.NoError(t, err)
   189  
   190  	transaction.GetTransactionID()
   191  	transaction.GetNodeAccountIDs()
   192  
   193  	proto := transaction.build().GetConsensusUpdateTopic()
   194  	require.Equal(t, proto.AdminKey.String(), newKey._ToProtoKey().String())
   195  	require.Equal(t, proto.TopicID.String(), topicID._ToProtobuf().String())
   196  	require.Equal(t, proto.AutoRenewAccount.String(), accountID._ToProtobuf().String())
   197  	require.Equal(t, proto.SubmitKey.String(), newKey2._ToProtoKey().String())
   198  	require.Equal(t, proto.Memo.Value, "memo")
   199  	require.Equal(t, proto.AutoRenewPeriod.Seconds, _DurationToProtobuf(time.Second*3).Seconds)
   200  	require.Equal(t, proto.ExpirationTime.String(), _TimeToProtobuf(time.Unix(34, 12)).String())
   201  }
   202  
   203  func TestUnitTopicUpdateTransactionCoverage(t *testing.T) {
   204  	t.Parallel()
   205  
   206  	checksum := "dmqui"
   207  	grpc := time.Second * 30
   208  	account := AccountID{Account: 3, checksum: &checksum}
   209  	topic := TopicID{Topic: 3, checksum: &checksum}
   210  	nodeAccountID := []AccountID{{Account: 10}}
   211  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
   212  
   213  	newKey, err := PrivateKeyGenerateEd25519()
   214  	require.NoError(t, err)
   215  
   216  	client, err := _NewMockClient()
   217  	client.SetLedgerID(*NewLedgerIDTestnet())
   218  	require.NoError(t, err)
   219  	client.SetAutoValidateChecksums(true)
   220  
   221  	transaction, err := NewTopicUpdateTransaction().
   222  		SetTransactionID(transactionID).
   223  		SetNodeAccountIDs(nodeAccountID).
   224  		SetTopicID(topic).
   225  		SetAdminKey(newKey).
   226  		SetTopicMemo("ad").
   227  		SetSubmitKey(newKey).
   228  		SetAutoRenewAccountID(account).
   229  		SetAutoRenewPeriod(time.Second * 30).
   230  		SetGrpcDeadline(&grpc).
   231  		SetMaxTransactionFee(NewHbar(3)).
   232  		SetMaxRetry(3).
   233  		SetMaxBackoff(time.Second * 30).
   234  		SetMinBackoff(time.Second * 10).
   235  		SetTransactionMemo("no").
   236  		SetTransactionValidDuration(time.Second * 30).
   237  		SetRegenerateTransactionID(false).
   238  		Freeze()
   239  	require.NoError(t, err)
   240  
   241  	err = transaction.validateNetworkOnIDs(client)
   242  	require.NoError(t, err)
   243  	_, err = transaction.Schedule()
   244  	require.NoError(t, err)
   245  	transaction.GetTransactionID()
   246  	transaction.GetNodeAccountIDs()
   247  	transaction.GetMaxRetry()
   248  	transaction.GetMaxTransactionFee()
   249  	transaction.GetMaxBackoff()
   250  	transaction.GetMinBackoff()
   251  	transaction.GetRegenerateTransactionID()
   252  	byt, err := transaction.ToBytes()
   253  	require.NoError(t, err)
   254  	txFromBytes, err := TransactionFromBytes(byt)
   255  	require.NoError(t, err)
   256  	sig, err := newKey.SignTransaction(&transaction.Transaction)
   257  	require.NoError(t, err)
   258  
   259  	_, err = transaction.GetTransactionHash()
   260  	require.NoError(t, err)
   261  	transaction.GetMaxTransactionFee()
   262  	transaction.GetTransactionMemo()
   263  	transaction.GetRegenerateTransactionID()
   264  	transaction.GetAdminKey()
   265  	transaction.GetSubmitKey()
   266  	transaction.GetTopicMemo()
   267  	transaction.GetAutoRenewAccountID()
   268  	transaction.GetAutoRenewPeriod()
   269  	_, err = transaction.GetSignatures()
   270  	require.NoError(t, err)
   271  	transaction.getName()
   272  	switch b := txFromBytes.(type) {
   273  	case TopicCreateTransaction:
   274  		b.AddSignature(newKey.PublicKey(), sig)
   275  	}
   276  }
   277  
   278  func TestUnitTopicUpdateTransactionMock(t *testing.T) {
   279  	t.Parallel()
   280  
   281  	newKey, err := PrivateKeyFromStringEd25519("302e020100300506032b657004220420a869f4c6191b9c8c99933e7f6b6611711737e4b1a1a5a4cb5370e719a1f6df98")
   282  	require.NoError(t, err)
   283  
   284  	call := func(request *services.Transaction) *services.TransactionResponse {
   285  		require.NotEmpty(t, request.SignedTransactionBytes)
   286  		signedTransaction := services.SignedTransaction{}
   287  		_ = protobuf.Unmarshal(request.SignedTransactionBytes, &signedTransaction)
   288  
   289  		require.NotEmpty(t, signedTransaction.BodyBytes)
   290  		transactionBody := services.TransactionBody{}
   291  		_ = protobuf.Unmarshal(signedTransaction.BodyBytes, &transactionBody)
   292  
   293  		require.NotNil(t, transactionBody.TransactionID)
   294  		transactionId := transactionBody.TransactionID.String()
   295  		require.NotEqual(t, "", transactionId)
   296  
   297  		sigMap := signedTransaction.GetSigMap()
   298  		require.NotNil(t, sigMap)
   299  
   300  		return &services.TransactionResponse{
   301  			NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK,
   302  		}
   303  	}
   304  	responses := [][]interface{}{{
   305  		call,
   306  	}}
   307  
   308  	client, server := NewMockClientAndServer(responses)
   309  	defer server.Close()
   310  
   311  	checksum := "dmqui"
   312  	topic := TopicID{Topic: 3, checksum: &checksum}
   313  
   314  	freez, err := NewTopicUpdateTransaction().
   315  		SetNodeAccountIDs([]AccountID{{Account: 3}}).
   316  		SetTopicID(topic).
   317  		SetAdminKey(newKey).
   318  		SetSubmitKey(newKey).
   319  		FreezeWith(client)
   320  	require.NoError(t, err)
   321  
   322  	_, err = freez.Sign(newKey).Execute(client)
   323  	require.NoError(t, err)
   324  }