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

Allow blank address in NotifyUTXOsChanged to get all updates #2027

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion app/rpc/rpccontext/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func NewContext(cfg *config.Config,
UTXOIndex: utxoIndex,
ShutDownChan: shutDownChan,
}
context.NotificationManager = NewNotificationManager()
context.NotificationManager = NewNotificationManager(cfg.ActiveNetParams)

return context
}
40 changes: 36 additions & 4 deletions app/rpc/rpccontext/notificationmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package rpccontext
import (
"sync"

"github.com/kaspanet/kaspad/domain/dagconfig"

"github.com/kaspanet/kaspad/domain/consensus/utils/txscript"

"github.com/kaspanet/kaspad/app/appmessage"
"github.com/kaspanet/kaspad/domain/utxoindex"
routerpkg "github.com/kaspanet/kaspad/infrastructure/network/netadapter/router"
Expand All @@ -13,6 +17,7 @@ import (
type NotificationManager struct {
sync.RWMutex
listeners map[*routerpkg.Router]*NotificationListener
params *dagconfig.Params
}

// UTXOsChangedNotificationAddress represents a kaspad address.
Expand All @@ -24,6 +29,8 @@ type UTXOsChangedNotificationAddress struct {

// NotificationListener represents a registered RPC notification listener
type NotificationListener struct {
params *dagconfig.Params

propagateBlockAddedNotifications bool
propagateVirtualSelectedParentChainChangedNotifications bool
propagateFinalityConflictNotifications bool
Expand All @@ -38,8 +45,9 @@ type NotificationListener struct {
}

// NewNotificationManager creates a new NotificationManager
func NewNotificationManager() *NotificationManager {
func NewNotificationManager(params *dagconfig.Params) *NotificationManager {
return &NotificationManager{
params: params,
listeners: make(map[*routerpkg.Router]*NotificationListener),
}
}
Expand All @@ -49,7 +57,7 @@ func (nm *NotificationManager) AddListener(router *routerpkg.Router) {
nm.Lock()
defer nm.Unlock()

listener := newNotificationListener()
listener := newNotificationListener(nm.params)
nm.listeners[router] = listener
}

Expand Down Expand Up @@ -238,8 +246,10 @@ func (nm *NotificationManager) NotifyPruningPointUTXOSetOverride() error {
return nil
}

func newNotificationListener() *NotificationListener {
func newNotificationListener(params *dagconfig.Params) *NotificationListener {
return &NotificationListener{
params: params,

propagateBlockAddedNotifications: false,
propagateVirtualSelectedParentChainChangedNotifications: false,
propagateFinalityConflictNotifications: false,
Expand Down Expand Up @@ -326,7 +336,7 @@ func (nl *NotificationListener) convertUTXOChangesToUTXOsChangedNotification(
notification.Removed = append(notification.Removed, utxosByAddressesEntries...)
}
}
} else {
} else if addressesSize > 0 {
for _, listenerAddress := range nl.propagateUTXOsChangedNotificationAddresses {
listenerScriptPublicKeyString := listenerAddress.ScriptPublicKeyString
if addedPairs, ok := utxoChanges.Added[listenerScriptPublicKeyString]; ok {
Expand All @@ -338,11 +348,33 @@ func (nl *NotificationListener) convertUTXOChangesToUTXOsChangedNotification(
notification.Removed = append(notification.Removed, utxosByAddressesEntries...)
}
}
} else {
for scriptPublicKeyString, addedPairs := range utxoChanges.Added {
addressString := nl.scriptPubKeyStringToAddressString(scriptPublicKeyString)
utxosByAddressesEntries := ConvertUTXOOutpointEntryPairsToUTXOsByAddressesEntries(addressString, addedPairs)
notification.Added = append(notification.Added, utxosByAddressesEntries...)
}
for scriptPublicKeyString, removedOutpoints := range utxoChanges.Removed {
addressString := nl.scriptPubKeyStringToAddressString(scriptPublicKeyString)
utxosByAddressesEntries := convertUTXOOutpointsToUTXOsByAddressesEntries(addressString, removedOutpoints)
notification.Removed = append(notification.Removed, utxosByAddressesEntries...)
}
}

return notification
}

func (nl *NotificationListener) scriptPubKeyStringToAddressString(scriptPublicKeyString utxoindex.ScriptPublicKeyString) string {
scriptPubKey := utxoindex.ConvertStringToScriptPublicKey(scriptPublicKeyString)
_, address, err := txscript.ExtractScriptPubKeyAddress(scriptPubKey, nl.params)
var addressString string
if err != nil { // This just means the script is not standard
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ExtractScriptPubKeyAddress should just return NonStandardTy for non standard scripts, and no error

addressString = ""
}
addressString = address.String()
return addressString
}

// PropagateVirtualSelectedParentBlueScoreChangedNotifications instructs the listener to send
// virtual selected parent blue score notifications to the remote listener
func (nl *NotificationListener) PropagateVirtualSelectedParentBlueScoreChangedNotifications() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ message GetHeadersResponseMessage{
//
// See: UtxosChangedNotificationMessage
message NotifyUtxosChangedRequestMessage {
repeated string addresses = 1;
repeated string addresses = 1; // Leave empty to get all updates
}

message NotifyUtxosChangedResponseMessage {
Expand Down