Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: nil pointers identified by nilaway #154

Merged
merged 1 commit into from
Mar 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions internal/tx/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,24 @@ func createTx(blockData any, nonce [16]byte) ([]byte, error) {
return nil, err
}
var utxos []UTxO.UTxO
tunaPolicyId, _ := Policy.New(validatorHash)
tunaPolicyId, err := Policy.New(validatorHash)
if err != nil {
return nil, err
}
var tunaCount int64
for _, utxoBytes := range utxosBytes {
var utxo UTxO.UTxO
utxo := UTxO.UTxO{}
if _, err := cbor.Decode(utxoBytes, &utxo); err != nil {
return nil, err
}
// Record the number of TUNA in inputs to use in outputs
tunaCount += utxo.Output.GetValue().
GetAssets().
GetByPolicyAndId(*tunaPolicyId, AssetName.NewAssetNameFromString("TUNA"))
assets := utxo.Output.GetValue().GetAssets()
if assets != nil {
tunaCount += assets.GetByPolicyAndId(
*tunaPolicyId,
AssetName.NewAssetNameFromString("TUNA"),
)
}
utxos = append(utxos, utxo)
}

Expand All @@ -110,7 +117,7 @@ func createTx(blockData any, nonce [16]byte) ([]byte, error) {
if err != nil {
return nil, err
}
var scriptUtxos []UTxO.UTxO
scriptUtxos := []UTxO.UTxO{}
for _, utxoBytes := range scriptUtxosBytes {
var utxo UTxO.UTxO
if _, err := cbor.Decode(utxoBytes, &utxo); err != nil {
Expand Down Expand Up @@ -268,7 +275,7 @@ func submitTxNtN(txRawBytes []byte) (string, error) {

// Generate TX hash
// Unwrap raw transaction bytes into a CBOR array
var txUnwrap []cbor.RawMessage
txUnwrap := []cbor.RawMessage{}
if _, err := cbor.Decode(txRawBytes, &txUnwrap); err != nil {
logger.Errorf("failed to unwrap transaction CBOR: %s", err)
return "", fmt.Errorf("failed to unwrap transaction CBOR: %s", err)
Expand Down Expand Up @@ -333,20 +340,28 @@ func submitTxApi(txRawBytes []byte) (string, error) {
return "", fmt.Errorf("failed to create request: %s", err)
}
req.Header.Add("Content-Type", "application/cbor")
resp, err := http.DefaultClient.Do(req)
client := &http.Client{Timeout: 5 * time.Minute}
resp, err := client.Do(req)
if err != nil {
return "", fmt.Errorf(
"failed to send request: %s: %s",
cfg.Submit.Url,
err,
)
}
if resp == nil {
return "", fmt.Errorf(
"failed parsing empty response from: %s",
cfg.Submit.Url,
)
}
// We have to read the entire response body and close it to prevent a memory leak
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("failed to read response body: %s", err)
}
defer resp.Body.Close()

if resp.StatusCode == 202 {
return string(respBody), nil
} else {
Expand Down