github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/auth/client/utils/tx_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"io/ioutil"
     7  	"os"
     8  	"testing"
     9  
    10  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec"
    11  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    12  	authtypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/types"
    13  	"github.com/fibonacci-chain/fbc/libs/tendermint/crypto/ed25519"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  var (
    18  	priv = ed25519.GenPrivKey()
    19  	addr = sdk.AccAddress(priv.PubKey().Address())
    20  )
    21  
    22  func TestParseQueryResponse(t *testing.T) {
    23  	cdc := makeCodec()
    24  	simRes := sdk.SimulationResponse{
    25  		GasInfo: sdk.GasInfo{GasUsed: 10, GasWanted: 20},
    26  		Result:  &sdk.Result{Data: []byte("tx data"), Log: "log"},
    27  	}
    28  
    29  	bz := cdc.MustMarshalBinaryBare(simRes)
    30  	res, err := parseQueryResponse(cdc, bz)
    31  	require.NoError(t, err)
    32  	require.Equal(t, 10, int(res.GasInfo.GasUsed))
    33  	require.NotNil(t, res.Result)
    34  
    35  	res, err = parseQueryResponse(cdc, []byte("fuzzy"))
    36  	require.Error(t, err)
    37  }
    38  
    39  func TestCalculateGas(t *testing.T) {
    40  	cdc := makeCodec()
    41  	makeQueryFunc := func(gasUsed uint64, wantErr bool) func(string, []byte) ([]byte, int64, error) {
    42  		return func(string, []byte) ([]byte, int64, error) {
    43  			if wantErr {
    44  				return nil, 0, errors.New("query failed")
    45  			}
    46  			simRes := sdk.SimulationResponse{
    47  				GasInfo: sdk.GasInfo{GasUsed: gasUsed, GasWanted: gasUsed},
    48  				Result:  &sdk.Result{Data: []byte("tx data"), Log: "log"},
    49  			}
    50  
    51  			return cdc.MustMarshalBinaryBare(simRes), 0, nil
    52  		}
    53  	}
    54  
    55  	type args struct {
    56  		queryFuncGasUsed uint64
    57  		queryFuncWantErr bool
    58  		adjustment       float64
    59  	}
    60  
    61  	tests := []struct {
    62  		name         string
    63  		args         args
    64  		wantEstimate uint64
    65  		wantAdjusted uint64
    66  		expPass      bool
    67  	}{
    68  		{"error", args{0, true, 1.2}, 0, 0, false},
    69  		{"adjusted gas", args{10, false, 1.2}, 10, 12, true},
    70  	}
    71  
    72  	for _, tt := range tests {
    73  		tt := tt
    74  		t.Run(tt.name, func(t *testing.T) {
    75  			queryFunc := makeQueryFunc(tt.args.queryFuncGasUsed, tt.args.queryFuncWantErr)
    76  			simRes, gotAdjusted, err := CalculateGas(queryFunc, cdc, []byte(""), tt.args.adjustment)
    77  			if tt.expPass {
    78  				require.NoError(t, err)
    79  				require.Equal(t, simRes.GasInfo.GasUsed, tt.wantEstimate)
    80  				require.Equal(t, gotAdjusted, tt.wantAdjusted)
    81  				require.NotNil(t, simRes.Result)
    82  			} else {
    83  				require.Error(t, err)
    84  				require.Nil(t, simRes.Result)
    85  			}
    86  		})
    87  	}
    88  }
    89  
    90  func TestDefaultTxEncoder(t *testing.T) {
    91  	cdc := makeCodec()
    92  
    93  	defaultEncoder := authtypes.DefaultTxEncoder(cdc)
    94  	encoder := GetTxEncoder(cdc)
    95  
    96  	compareEncoders(t, defaultEncoder, encoder)
    97  }
    98  
    99  func TestEthereumTxEncoder(t *testing.T) {
   100  	ethereumTxEncoder := authtypes.EthereumTxEncoder(nil)
   101  	encoder := GetTxEncoder(nil, WithEthereumTx())
   102  
   103  	compareEncoders(t, ethereumTxEncoder, encoder)
   104  }
   105  
   106  func TestConfiguredTxEncoder(t *testing.T) {
   107  	cdc := makeCodec()
   108  
   109  	customEncoder := func(tx sdk.Tx) ([]byte, error) {
   110  		return json.Marshal(tx)
   111  	}
   112  
   113  	config := sdk.GetConfig()
   114  	config.SetTxEncoder(customEncoder)
   115  
   116  	encoder := GetTxEncoder(cdc)
   117  
   118  	compareEncoders(t, customEncoder, encoder)
   119  }
   120  
   121  func TestReadStdTxFromFile(t *testing.T) {
   122  	cdc := codec.New()
   123  	sdk.RegisterCodec(cdc)
   124  
   125  	// Build a test transaction
   126  	fee := authtypes.NewStdFee(50000, sdk.Coins{sdk.NewInt64Coin("atom", 150)})
   127  	stdTx := authtypes.NewStdTx([]sdk.Msg{}, fee, []authtypes.StdSignature{}, "foomemo")
   128  
   129  	// Write it to the file
   130  	encodedTx, _ := cdc.MarshalJSON(stdTx)
   131  	jsonTxFile := writeToNewTempFile(t, string(encodedTx))
   132  	defer os.Remove(jsonTxFile.Name())
   133  
   134  	// Read it back
   135  	decodedTx, err := ReadStdTxFromFile(cdc, jsonTxFile.Name())
   136  	require.NoError(t, err)
   137  	require.Equal(t, decodedTx.Memo, "foomemo")
   138  }
   139  
   140  func compareEncoders(t *testing.T, expected sdk.TxEncoder, actual sdk.TxEncoder) {
   141  	msgs := []sdk.Msg{sdk.NewTestMsg(addr)}
   142  	tx := authtypes.NewStdTx(msgs, authtypes.StdFee{}, []authtypes.StdSignature{}, "")
   143  
   144  	defaultEncoderBytes, err := expected(tx)
   145  	require.NoError(t, err)
   146  	encoderBytes, err := actual(tx)
   147  	require.NoError(t, err)
   148  	require.Equal(t, defaultEncoderBytes, encoderBytes)
   149  }
   150  
   151  func writeToNewTempFile(t *testing.T, data string) *os.File {
   152  	fp, err := ioutil.TempFile(os.TempDir(), "client_tx_test")
   153  	require.NoError(t, err)
   154  
   155  	_, err = fp.WriteString(data)
   156  	require.NoError(t, err)
   157  
   158  	return fp
   159  }
   160  
   161  func makeCodec() *codec.Codec {
   162  	var cdc = codec.New()
   163  	sdk.RegisterCodec(cdc)
   164  	codec.RegisterCrypto(cdc)
   165  	authtypes.RegisterCodec(cdc)
   166  	cdc.RegisterConcrete(sdk.TestMsg{}, "cosmos-sdk/Test", nil)
   167  	return cdc
   168  }