Skip to content

Commit

Permalink
Don't swallow errors
Browse files Browse the repository at this point in the history
  • Loading branch information
someone235 committed May 18, 2022
1 parent b070899 commit 93af84b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
32 changes: 23 additions & 9 deletions app/rpc/rpccontext/notificationmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,18 @@ func (nm *NotificationManager) NotifyUTXOsChanged(utxoChanges *utxoindex.UTXOCha
for router, listener := range nm.listeners {
if listener.propagateUTXOsChangedNotifications {
// Filter utxoChanges and create a notification
notification := listener.convertUTXOChangesToUTXOsChangedNotification(utxoChanges)
notification, err := listener.convertUTXOChangesToUTXOsChangedNotification(utxoChanges)
if err != nil {
return err
}

// Don't send the notification if it's empty
if len(notification.Added) == 0 && len(notification.Removed) == 0 {
continue
}

// Enqueue the notification
err := router.OutgoingRoute().Enqueue(notification)
err = router.OutgoingRoute().Enqueue(notification)
if err != nil {
return err
}
Expand Down Expand Up @@ -349,7 +352,7 @@ func (nl *NotificationListener) StopPropagatingUTXOsChangedNotifications(address
}

func (nl *NotificationListener) convertUTXOChangesToUTXOsChangedNotification(
utxoChanges *utxoindex.UTXOChanges) *appmessage.UTXOsChangedNotificationMessage {
utxoChanges *utxoindex.UTXOChanges) (*appmessage.UTXOsChangedNotificationMessage, error) {

// As an optimization, we iterate over the smaller set (O(n)) among the two below
// and check existence over the larger set (O(1))
Expand Down Expand Up @@ -384,33 +387,44 @@ func (nl *NotificationListener) convertUTXOChangesToUTXOsChangedNotification(
}
} else {
for scriptPublicKeyString, addedPairs := range utxoChanges.Added {
addressString := nl.scriptPubKeyStringToAddressString(scriptPublicKeyString)
addressString, err := nl.scriptPubKeyStringToAddressString(scriptPublicKeyString)
if err != nil {
return nil, err
}

utxosByAddressesEntries := ConvertUTXOOutpointEntryPairsToUTXOsByAddressesEntries(addressString, addedPairs)
notification.Added = append(notification.Added, utxosByAddressesEntries...)
}
for scriptPublicKeyString, removedOutpoints := range utxoChanges.Removed {
addressString := nl.scriptPubKeyStringToAddressString(scriptPublicKeyString)
addressString, err := nl.scriptPubKeyStringToAddressString(scriptPublicKeyString)
if err != nil {
return nil, err
}

utxosByAddressesEntries := convertUTXOOutpointsToUTXOsByAddressesEntries(addressString, removedOutpoints)
notification.Removed = append(notification.Removed, utxosByAddressesEntries...)
}
}

return notification
return notification, nil
}

func (nl *NotificationListener) scriptPubKeyStringToAddressString(scriptPublicKeyString utxoindex.ScriptPublicKeyString) string {
func (nl *NotificationListener) scriptPubKeyStringToAddressString(scriptPublicKeyString utxoindex.ScriptPublicKeyString) (string, error) {
scriptPubKey := utxoindex.ConvertStringToScriptPublicKey(scriptPublicKeyString)

// ignore error because it is often returned when the script is of unknown type
scriptType, address, _ := txscript.ExtractScriptPubKeyAddress(scriptPubKey, nl.params)
scriptType, address, err := txscript.ExtractScriptPubKeyAddress(scriptPubKey, nl.params)
if err != nil {
return "", err
}

var addressString string
if scriptType == txscript.NonStandardTy {
addressString = ""
} else {
addressString = address.String()
}
return addressString
return addressString, nil
}

// PropagateVirtualSelectedParentBlueScoreChangedNotifications instructs the listener to send
Expand Down
2 changes: 1 addition & 1 deletion domain/consensus/utils/txscript/standard.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ func PushedData(script []byte) ([][]byte, error) {
// as public keys which are invalid will return a nil address.
func ExtractScriptPubKeyAddress(scriptPubKey *externalapi.ScriptPublicKey, dagParams *dagconfig.Params) (ScriptClass, util.Address, error) {
if scriptPubKey.Version > constants.MaxScriptPublicKeyVersion {
return NonStandardTy, nil, errors.Errorf("Script version is unknown.")
return NonStandardTy, nil, nil
}
// No valid address if the script doesn't parse.
pops, err := parseScript(scriptPubKey.Script)
Expand Down

0 comments on commit 93af84b

Please sign in to comment.