github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/ethereum_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  	"encoding/hex"
    28  	"testing"
    29  	"time"
    30  
    31  	"github.com/hashgraph/hedera-protobufs-go/services"
    32  	protobuf "google.golang.org/protobuf/proto"
    33  
    34  	"github.com/stretchr/testify/require"
    35  )
    36  
    37  func TestUnitEthereumTransactionMock(t *testing.T) {
    38  	t.Parallel()
    39  
    40  	call := func(request *services.Transaction) *services.TransactionResponse {
    41  		require.NotEmpty(t, request.SignedTransactionBytes)
    42  		signedTransaction := services.SignedTransaction{}
    43  		_ = protobuf.Unmarshal(request.SignedTransactionBytes, &signedTransaction)
    44  
    45  		require.NotEmpty(t, signedTransaction.BodyBytes)
    46  		transactionBody := services.TransactionBody{}
    47  		_ = protobuf.Unmarshal(signedTransaction.BodyBytes, &transactionBody)
    48  
    49  		require.NotNil(t, transactionBody.TransactionID)
    50  		transactionId := transactionBody.TransactionID.String()
    51  		require.NotEqual(t, "", transactionId)
    52  
    53  		sigMap := signedTransaction.GetSigMap()
    54  		require.NotNil(t, sigMap)
    55  
    56  		return &services.TransactionResponse{
    57  			NodeTransactionPrecheckCode: services.ResponseCodeEnum_OK,
    58  		}
    59  	}
    60  	responses := [][]interface{}{{
    61  		call,
    62  	}}
    63  
    64  	client, server := NewMockClientAndServer(responses)
    65  	defer server.Close()
    66  
    67  	byt, err := hex.DecodeString("02f87082012a022f2f83018000947e3a9eaf9bcc39e2ffa38eb30bf7a93feacbc181880de0b6b3a764000083123456c001a0df48f2efd10421811de2bfb125ab75b2d3c44139c4642837fb1fccce911fd479a01aaf7ae92bee896651dfc9d99ae422a296bf5d9f1ca49b2d96d82b79eb112d66")
    68  	require.NoError(t, err)
    69  	b, err := EthereumTransactionDataFromBytes(byt)
    70  	toByt, err := b.ToBytes()
    71  
    72  	tran := TransactionIDGenerate(AccountID{Account: 3})
    73  
    74  	_, err = NewEthereumTransaction().
    75  		SetNodeAccountIDs([]AccountID{{Account: 3}}).
    76  		SetTransactionID(tran).
    77  		SetEthereumData(toByt).
    78  		Execute(client)
    79  	require.NoError(t, err)
    80  }
    81  
    82  func TestUnitEthereumTransactionCoverage(t *testing.T) {
    83  	t.Parallel()
    84  
    85  	checksum := "dmqui"
    86  	grpc := time.Second * 30
    87  	file := FileID{File: 3, checksum: &checksum}
    88  	nodeAccountID := []AccountID{{Account: 10}}
    89  	transactionID := TransactionIDGenerate(AccountID{Account: 324})
    90  
    91  	byt, err := hex.DecodeString("02f87082012a022f2f83018000947e3a9eaf9bcc39e2ffa38eb30bf7a93feacbc181880de0b6b3a764000083123456c001a0df48f2efd10421811de2bfb125ab75b2d3c44139c4642837fb1fccce911fd479a01aaf7ae92bee896651dfc9d99ae422a296bf5d9f1ca49b2d96d82b79eb112d66")
    92  	require.NoError(t, err)
    93  
    94  	newKey, err := PrivateKeyGenerateEd25519()
    95  	require.NoError(t, err)
    96  
    97  	client, err := _NewMockClient()
    98  	client.SetLedgerID(*NewLedgerIDTestnet())
    99  	require.NoError(t, err)
   100  	client.SetAutoValidateChecksums(true)
   101  
   102  	transaction, err := NewEthereumTransaction().
   103  		SetTransactionID(transactionID).
   104  		SetNodeAccountIDs(nodeAccountID).
   105  		SetEthereumData(byt).
   106  		SetCallDataFileID(file).
   107  		SetCallData(file).
   108  		SetMaxGasAllowed(234).
   109  		SetMaxGasAllowanceHbar(NewHbar(3)).
   110  		SetGrpcDeadline(&grpc).
   111  		SetMaxTransactionFee(NewHbar(3)).
   112  		SetMaxRetry(3).
   113  		SetMaxBackoff(time.Second * 30).
   114  		SetMinBackoff(time.Second * 10).
   115  		SetTransactionMemo("no").
   116  		SetTransactionValidDuration(time.Second * 30).
   117  		SetRegenerateTransactionID(false).
   118  		Freeze()
   119  	require.NoError(t, err)
   120  
   121  	transaction.validateNetworkOnIDs(client)
   122  
   123  	require.NoError(t, err)
   124  	transaction.GetTransactionID()
   125  	transaction.GetNodeAccountIDs()
   126  	transaction.GetMaxRetry()
   127  	transaction.GetMaxTransactionFee()
   128  	transaction.GetMaxBackoff()
   129  	transaction.GetMinBackoff()
   130  	transaction.GetRegenerateTransactionID()
   131  	byt, err = transaction.ToBytes()
   132  	require.NoError(t, err)
   133  	txFromBytes, err := TransactionFromBytes(byt)
   134  	require.NoError(t, err)
   135  	sig, err := newKey.SignTransaction(&transaction.Transaction)
   136  	require.NoError(t, err)
   137  
   138  	_, err = transaction.GetTransactionHash()
   139  	require.NoError(t, err)
   140  	transaction.GetMaxTransactionFee()
   141  	transaction.GetTransactionMemo()
   142  	transaction.GetRegenerateTransactionID()
   143  	transaction.GetEthereumData()
   144  	transaction.GetCallData()
   145  	transaction.GetMaxGasAllowed()
   146  	_, err = transaction.GetSignatures()
   147  	require.NoError(t, err)
   148  	transaction.getName()
   149  	switch b := txFromBytes.(type) {
   150  	case EthereumTransaction:
   151  		b.AddSignature(newKey.PublicKey(), sig)
   152  	}
   153  }