Skip to content

Commit

Permalink
removed panic from test method LoadObjectFromJSONFile
Browse files Browse the repository at this point in the history
  • Loading branch information
ws4charlie committed Mar 28, 2024
1 parent 2bcdfbc commit edc9be6
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 110 deletions.
82 changes: 41 additions & 41 deletions zetaclient/bitcoin/bitcoin_client_test.go

Large diffs are not rendered by default.

34 changes: 17 additions & 17 deletions zetaclient/bitcoin/tx_script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestDecodeVoutP2TR(t *testing.T) {
nameTx := path.Join("../", testutils.TestDataPathBTC, testutils.FileNameBTCTxByType(chain.ChainId, "P2TR", txHash))

var rawResult btcjson.TxRawResult
testutils.LoadObjectFromJSONFile(&rawResult, nameTx)
testutils.LoadObjectFromJSONFile(t, &rawResult, nameTx)
require.Len(t, rawResult.Vout, 2)

// decode vout 0, P2TR
Expand All @@ -40,7 +40,7 @@ func TestDecodeVoutP2TRErrors(t *testing.T) {
nameTx := path.Join("../", testutils.TestDataPathBTC, testutils.FileNameBTCTxByType(chain.ChainId, "P2TR", txHash))

var rawResult btcjson.TxRawResult
testutils.LoadObjectFromJSONFile(&rawResult, nameTx)
testutils.LoadObjectFromJSONFile(t, &rawResult, nameTx)

t.Run("should return error on invalid script", func(t *testing.T) {
invalidVout := rawResult.Vout[0]
Expand Down Expand Up @@ -79,7 +79,7 @@ func TestDecodeVoutP2WSH(t *testing.T) {
nameTx := path.Join("../", testutils.TestDataPathBTC, testutils.FileNameBTCTxByType(chain.ChainId, "P2WSH", txHash))

var rawResult btcjson.TxRawResult
testutils.LoadObjectFromJSONFile(&rawResult, nameTx)
testutils.LoadObjectFromJSONFile(t, &rawResult, nameTx)
require.Len(t, rawResult.Vout, 1)

// decode vout 0, P2WSH
Expand All @@ -97,7 +97,7 @@ func TestDecodeVoutP2WSHErrors(t *testing.T) {
nameTx := path.Join("../", testutils.TestDataPathBTC, testutils.FileNameBTCTxByType(chain.ChainId, "P2WSH", txHash))

var rawResult btcjson.TxRawResult
testutils.LoadObjectFromJSONFile(&rawResult, nameTx)
testutils.LoadObjectFromJSONFile(t, &rawResult, nameTx)

t.Run("should return error on invalid script", func(t *testing.T) {
invalidVout := rawResult.Vout[0]
Expand Down Expand Up @@ -136,7 +136,7 @@ func TestDecodeP2WPKHVout(t *testing.T) {
nameTx := path.Join("../", testutils.TestDataPathBTC, testutils.FileNameBTCOuttx(chain.ChainId, nonce))

var rawResult btcjson.TxRawResult
testutils.LoadObjectFromJSONFile(&rawResult, nameTx)
testutils.LoadObjectFromJSONFile(t, &rawResult, nameTx)
require.Len(t, rawResult.Vout, 3)

// decode vout 0, nonce mark 148
Expand Down Expand Up @@ -164,7 +164,7 @@ func TestDecodeP2WPKHVoutErrors(t *testing.T) {
nameTx := path.Join("../", testutils.TestDataPathBTC, testutils.FileNameBTCOuttx(chain.ChainId, nonce))

var rawResult btcjson.TxRawResult
testutils.LoadObjectFromJSONFile(&rawResult, nameTx)
testutils.LoadObjectFromJSONFile(t, &rawResult, nameTx)

t.Run("should return error on invalid script", func(t *testing.T) {
invalidVout := rawResult.Vout[0]
Expand Down Expand Up @@ -196,7 +196,7 @@ func TestDecodeVoutP2SH(t *testing.T) {
nameTx := path.Join("../", testutils.TestDataPathBTC, testutils.FileNameBTCTxByType(chain.ChainId, "P2SH", txHash))

var rawResult btcjson.TxRawResult
testutils.LoadObjectFromJSONFile(&rawResult, nameTx)
testutils.LoadObjectFromJSONFile(t, &rawResult, nameTx)
require.Len(t, rawResult.Vout, 2)

// decode vout 0, P2SH
Expand All @@ -214,7 +214,7 @@ func TestDecodeVoutP2SHErrors(t *testing.T) {
nameTx := path.Join("../", testutils.TestDataPathBTC, testutils.FileNameBTCTxByType(chain.ChainId, "P2SH", txHash))

var rawResult btcjson.TxRawResult
testutils.LoadObjectFromJSONFile(&rawResult, nameTx)
testutils.LoadObjectFromJSONFile(t, &rawResult, nameTx)

t.Run("should return error on invalid script", func(t *testing.T) {
invalidVout := rawResult.Vout[0]
Expand Down Expand Up @@ -259,7 +259,7 @@ func TestDecodeVoutP2PKH(t *testing.T) {
nameTx := path.Join("../", testutils.TestDataPathBTC, testutils.FileNameBTCTxByType(chain.ChainId, "P2PKH", txHash))

var rawResult btcjson.TxRawResult
testutils.LoadObjectFromJSONFile(&rawResult, nameTx)
testutils.LoadObjectFromJSONFile(t, &rawResult, nameTx)
require.Len(t, rawResult.Vout, 2)

// decode vout 0, P2PKH
Expand All @@ -277,7 +277,7 @@ func TestDecodeVoutP2PKHErrors(t *testing.T) {
nameTx := path.Join("../", testutils.TestDataPathBTC, testutils.FileNameBTCTxByType(chain.ChainId, "P2PKH", txHash))

var rawResult btcjson.TxRawResult
testutils.LoadObjectFromJSONFile(&rawResult, nameTx)
testutils.LoadObjectFromJSONFile(t, &rawResult, nameTx)

t.Run("should return error on invalid script", func(t *testing.T) {
invalidVout := rawResult.Vout[0]
Expand Down Expand Up @@ -334,7 +334,7 @@ func TestDecodeOpReturnMemo(t *testing.T) {
chain := common.BtcMainnetChain()
txHash := "847139aa65aa4a5ee896375951cbf7417cfc8a4d6f277ec11f40cd87319f04aa"
scriptHex := "6a1467ed0bcc4e1256bc2ce87d22e190d63a120114bf"
rawResult := testutils.LoadBTCIntxRawResult(chain.ChainId, txHash, false)
rawResult := testutils.LoadBTCIntxRawResult(t, chain.ChainId, txHash, false)
require.True(t, len(rawResult.Vout) >= 2)
require.Equal(t, scriptHex, rawResult.Vout[1].ScriptPubKey.Hex)

Expand Down Expand Up @@ -410,7 +410,7 @@ func TestDecodeTSSVout(t *testing.T) {
txHash := "259fc21e63e138136c8f19270a0f7ca10039a66a474f91d23a17896f46e677a7"
nameTx := path.Join("../", testutils.TestDataPathBTC, testutils.FileNameBTCTxByType(chain.ChainId, "P2TR", txHash))
var rawResult btcjson.TxRawResult
testutils.LoadObjectFromJSONFile(&rawResult, nameTx)
testutils.LoadObjectFromJSONFile(t, &rawResult, nameTx)

receiverExpected := "bc1p4scddlkkuw9486579autxumxmkvuphm5pz4jvf7f6pdh50p2uzqstawjt9"
receiver, amount, err := DecodeTSSVout(rawResult.Vout[0], receiverExpected, chain)
Expand All @@ -423,7 +423,7 @@ func TestDecodeTSSVout(t *testing.T) {
txHash := "791bb9d16f7ab05f70a116d18eaf3552faf77b9d5688699a480261424b4f7e53"
nameTx := path.Join("../", testutils.TestDataPathBTC, testutils.FileNameBTCTxByType(chain.ChainId, "P2WSH", txHash))
var rawResult btcjson.TxRawResult
testutils.LoadObjectFromJSONFile(&rawResult, nameTx)
testutils.LoadObjectFromJSONFile(t, &rawResult, nameTx)

receiverExpected := "bc1qqv6pwn470vu0tssdfha4zdk89v3c8ch5lsnyy855k9hcrcv3evequdmjmc"
receiver, amount, err := DecodeTSSVout(rawResult.Vout[0], receiverExpected, chain)
Expand All @@ -436,7 +436,7 @@ func TestDecodeTSSVout(t *testing.T) {
txHash := "5d09d232bfe41c7cb831bf53fc2e4029ab33a99087fd5328a2331b52ff2ebe5b"
nameTx := path.Join("../", testutils.TestDataPathBTC, testutils.FileNameBTCTxByType(chain.ChainId, "P2WPKH", txHash))
var rawResult btcjson.TxRawResult
testutils.LoadObjectFromJSONFile(&rawResult, nameTx)
testutils.LoadObjectFromJSONFile(t, &rawResult, nameTx)

receiverExpected := "bc1qaxf82vyzy8y80v000e7t64gpten7gawewzu42y"
receiver, amount, err := DecodeTSSVout(rawResult.Vout[0], receiverExpected, chain)
Expand All @@ -449,7 +449,7 @@ func TestDecodeTSSVout(t *testing.T) {
txHash := "fd68c8b4478686ca6f5ae4c28eaab055490650dbdaa6c2c8e380a7e075958a21"
nameTx := path.Join("../", testutils.TestDataPathBTC, testutils.FileNameBTCTxByType(chain.ChainId, "P2SH", txHash))
var rawResult btcjson.TxRawResult
testutils.LoadObjectFromJSONFile(&rawResult, nameTx)
testutils.LoadObjectFromJSONFile(t, &rawResult, nameTx)

receiverExpected := "327z4GyFM8Y8DiYfasGKQWhRK4MvyMSEgE"
receiver, amount, err := DecodeTSSVout(rawResult.Vout[0], receiverExpected, chain)
Expand All @@ -462,7 +462,7 @@ func TestDecodeTSSVout(t *testing.T) {
txHash := "9c741de6e17382b7a9113fc811e3558981a35a360e3d1262a6675892c91322ca"
nameTx := path.Join("../", testutils.TestDataPathBTC, testutils.FileNameBTCTxByType(chain.ChainId, "P2PKH", txHash))
var rawResult btcjson.TxRawResult
testutils.LoadObjectFromJSONFile(&rawResult, nameTx)
testutils.LoadObjectFromJSONFile(t, &rawResult, nameTx)

receiverExpected := "1FueivsE338W2LgifJ25HhTcVJ7CRT8kte"
receiver, amount, err := DecodeTSSVout(rawResult.Vout[0], receiverExpected, chain)
Expand All @@ -480,7 +480,7 @@ func TestDecodeTSSVoutErrors(t *testing.T) {
nameTx := path.Join("../", testutils.TestDataPathBTC, testutils.FileNameBTCTxByType(chain.ChainId, "P2TR", txHash))

var rawResult btcjson.TxRawResult
testutils.LoadObjectFromJSONFile(&rawResult, nameTx)
testutils.LoadObjectFromJSONFile(t, &rawResult, nameTx)
receiverExpected := "bc1p4scddlkkuw9486579autxumxmkvuphm5pz4jvf7f6pdh50p2uzqstawjt9"

t.Run("should return error on invalid amount", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion zetaclient/compliance/compliance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func TestCctxRestricted(t *testing.T) {
// load archived cctx
var cctx crosschaintypes.CrossChainTx
testutils.LoadObjectFromJSONFile(&cctx, path.Join("../", testutils.TestDataPathCctx, "cctx_1_6270.json"))
testutils.LoadObjectFromJSONFile(t, &cctx, path.Join("../", testutils.TestDataPathCctx, "cctx_1_6270.json"))

// create config
cfg := config.Config{
Expand Down
2 changes: 1 addition & 1 deletion zetaclient/evm/evm_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestEVM_CheckTxInclusion(t *testing.T) {
// load archived evm block
// https://etherscan.io/block/19363323
blockNumber := receipt.BlockNumber.Uint64()
block := testutils.LoadEVMBlock(chainID, blockNumber, true)
block := testutils.LoadEVMBlock(t, chainID, blockNumber, true)

// create client
blockCache, err := lru.New(1000)
Expand Down
26 changes: 13 additions & 13 deletions zetaclient/evm/evm_signer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ func getNewOutTxProcessor() *outtxprocessor.Processor {
return outtxprocessor.NewOutTxProcessorManager(logger)
}

func getCCTX() *crosschaintypes.CrossChainTx {
func getCCTX(t *testing.T) *crosschaintypes.CrossChainTx {
var cctx crosschaintypes.CrossChainTx
testutils.LoadObjectFromJSONFile(&cctx, path.Join("../", testutils.TestDataPathCctx, "cctx_56_68270.json"))
testutils.LoadObjectFromJSONFile(t, &cctx, path.Join("../", testutils.TestDataPathCctx, "cctx_56_68270.json"))
return &cctx
}

func getInvalidCCTX() *crosschaintypes.CrossChainTx {
func getInvalidCCTX(t *testing.T) *crosschaintypes.CrossChainTx {
var cctx crosschaintypes.CrossChainTx
testutils.LoadObjectFromJSONFile(&cctx, path.Join("../", testutils.TestDataPathCctx, "cctx_56_68270_invalidChainID.json"))
testutils.LoadObjectFromJSONFile(t, &cctx, path.Join("../", testutils.TestDataPathCctx, "cctx_56_68270_invalidChainID.json"))
return &cctx
}

Expand Down Expand Up @@ -104,7 +104,7 @@ func TestSigner_SetGetERC20CustodyAddress(t *testing.T) {
func TestSigner_TryProcessOutTx(t *testing.T) {
evmSigner, err := getNewEvmSigner()
require.NoError(t, err)
cctx := getCCTX()
cctx := getCCTX(t)
processorManager := getNewOutTxProcessor()
mockChainClient, err := getNewEvmChainClient()
require.NoError(t, err)
Expand All @@ -123,7 +123,7 @@ func TestSigner_SignOutboundTx(t *testing.T) {

// Setup txData struct

cctx := getCCTX()
cctx := getCCTX(t)
mockChainClient, err := getNewEvmChainClient()
require.NoError(t, err)
txData, skip, err := NewOutBoundTransactionData(cctx, mockChainClient, evmSigner.EvmClient(), zerolog.Logger{}, 123)
Expand Down Expand Up @@ -152,7 +152,7 @@ func TestSigner_SignRevertTx(t *testing.T) {
require.NoError(t, err)

// Setup txData struct
cctx := getCCTX()
cctx := getCCTX(t)
mockChainClient, err := getNewEvmChainClient()
require.NoError(t, err)
txData, skip, err := NewOutBoundTransactionData(cctx, mockChainClient, evmSigner.EvmClient(), zerolog.Logger{}, 123)
Expand Down Expand Up @@ -181,7 +181,7 @@ func TestSigner_SignWithdrawTx(t *testing.T) {
require.NoError(t, err)

// Setup txData struct
cctx := getCCTX()
cctx := getCCTX(t)
mockChainClient, err := getNewEvmChainClient()
require.NoError(t, err)
txData, skip, err := NewOutBoundTransactionData(cctx, mockChainClient, evmSigner.EvmClient(), zerolog.Logger{}, 123)
Expand Down Expand Up @@ -210,7 +210,7 @@ func TestSigner_SignCommandTx(t *testing.T) {
require.NoError(t, err)

// Setup txData struct
cctx := getCCTX()
cctx := getCCTX(t)
mockChainClient, err := getNewEvmChainClient()
require.NoError(t, err)
txData, skip, err := NewOutBoundTransactionData(cctx, mockChainClient, evmSigner.EvmClient(), zerolog.Logger{}, 123)
Expand Down Expand Up @@ -257,7 +257,7 @@ func TestSigner_SignERC20WithdrawTx(t *testing.T) {
require.NoError(t, err)

// Setup txData struct
cctx := getCCTX()
cctx := getCCTX(t)
mockChainClient, err := getNewEvmChainClient()
require.NoError(t, err)
txData, skip, err := NewOutBoundTransactionData(cctx, mockChainClient, evmSigner.EvmClient(), zerolog.Logger{}, 123)
Expand Down Expand Up @@ -286,7 +286,7 @@ func TestSigner_BroadcastOutTx(t *testing.T) {
require.NoError(t, err)

// Setup txData struct
cctx := getCCTX()
cctx := getCCTX(t)
mockChainClient, err := getNewEvmChainClient()
require.NoError(t, err)
txData, skip, err := NewOutBoundTransactionData(cctx, mockChainClient, evmSigner.EvmClient(), zerolog.Logger{}, 123)
Expand Down Expand Up @@ -316,7 +316,7 @@ func TestSigner_getEVMRPC(t *testing.T) {
}

func TestSigner_SignerErrorMsg(t *testing.T) {
cctx := getCCTX()
cctx := getCCTX(t)

msg := SignerErrorMsg(cctx)
require.Contains(t, msg, "nonce 68270 chain 56")
Expand All @@ -328,7 +328,7 @@ func TestSigner_SignWhitelistERC20Cmd(t *testing.T) {
require.NoError(t, err)

// Setup txData struct
cctx := getCCTX()
cctx := getCCTX(t)
mockChainClient, err := getNewEvmChainClient()
require.NoError(t, err)
txData, skip, err := NewOutBoundTransactionData(cctx, mockChainClient, evmSigner.EvmClient(), zerolog.Logger{}, 123)
Expand Down
2 changes: 1 addition & 1 deletion zetaclient/evm/inbounds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ func TestEVM_ObserveTSSReceiveInBlock(t *testing.T) {
// load archived evm block
// https://etherscan.io/block/19363323
blockNumber := receipt.BlockNumber.Uint64()
block := testutils.LoadEVMBlock(chainID, blockNumber, true)
block := testutils.LoadEVMBlock(t, chainID, blockNumber, true)

// create mock client
evmClient := stub.NewMockEvmClient()
Expand Down
12 changes: 6 additions & 6 deletions zetaclient/evm/outbound_transaction_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

func TestSigner_SetChainAndSender(t *testing.T) {
// setup inputs
cctx := getCCTX()
cctx := getCCTX(t)
txData := &OutBoundTransactionData{}
logger := zerolog.Logger{}

Expand Down Expand Up @@ -43,7 +43,7 @@ func TestSigner_SetChainAndSender(t *testing.T) {
}

func TestSigner_SetupGas(t *testing.T) {
cctx := getCCTX()
cctx := getCCTX(t)
evmSigner, err := getNewEvmSigner()
require.NoError(t, err)

Expand Down Expand Up @@ -73,30 +73,30 @@ func TestSigner_NewOutBoundTransactionData(t *testing.T) {
require.NoError(t, err)

t.Run("NewOutBoundTransactionData success", func(t *testing.T) {
cctx := getCCTX()
cctx := getCCTX(t)
_, skip, err := NewOutBoundTransactionData(cctx, mockChainClient, evmSigner.EvmClient(), zerolog.Logger{}, 123)
require.False(t, skip)
require.NoError(t, err)
})

t.Run("NewOutBoundTransactionData skip", func(t *testing.T) {
cctx := getCCTX()
cctx := getCCTX(t)
cctx.CctxStatus.Status = types.CctxStatus_Aborted
_, skip, err := NewOutBoundTransactionData(cctx, mockChainClient, evmSigner.EvmClient(), zerolog.Logger{}, 123)
require.NoError(t, err)
require.True(t, skip)
})

t.Run("NewOutBoundTransactionData unknown chain", func(t *testing.T) {
cctx := getInvalidCCTX()
cctx := getInvalidCCTX(t)
require.NoError(t, err)
_, skip, err := NewOutBoundTransactionData(cctx, mockChainClient, evmSigner.EvmClient(), zerolog.Logger{}, 123)
require.ErrorContains(t, err, "unknown chain")
require.True(t, skip)
})

t.Run("NewOutBoundTransactionData setup gas error", func(t *testing.T) {
cctx := getCCTX()
cctx := getCCTX(t)
require.NoError(t, err)
cctx.GetCurrentOutTxParam().OutboundTxGasPrice = "invalidGasPrice"
_, skip, err := NewOutBoundTransactionData(cctx, mockChainClient, evmSigner.EvmClient(), zerolog.Logger{}, 123)
Expand Down
Loading

0 comments on commit edc9be6

Please sign in to comment.