mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-09-13 21:10:12 +00:00

* Add acceptedTransactionIds to GetVirtualSelectedParentChainFromBlockResponseMessage and VirtualSelectedParentChainChangedNotificationMessage * Modify appmessage structs to include new fields * Implement the functionality for acceptedTransactionID notifications * Add missing field for IncludeAcceptedTransactionIds * Notify of block added before notifying that chain changed * Use consensushashing instead of Transaction.ID * Don't notify of empty virtual changes * Don't generate virtualChainChanged notification if there's nobody subscribed * Fix test to not expect empty notifications * Don't generate acceptedTransactionIDs if they were not requested by anyone Co-authored-by: Ori Newman <orinewman1@gmail.com>
65 lines
2.5 KiB
Go
65 lines
2.5 KiB
Go
package rpccontext
|
|
|
|
import (
|
|
"github.com/kaspanet/kaspad/app/appmessage"
|
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/consensushashing"
|
|
)
|
|
|
|
// ConvertVirtualSelectedParentChainChangesToChainChangedNotificationMessage converts
|
|
// VirtualSelectedParentChainChanges to VirtualSelectedParentChainChangedNotificationMessage
|
|
func (ctx *Context) ConvertVirtualSelectedParentChainChangesToChainChangedNotificationMessage(
|
|
selectedParentChainChanges *externalapi.SelectedChainPath, includeAcceptedTransactionIDs bool) (
|
|
*appmessage.VirtualSelectedParentChainChangedNotificationMessage, error) {
|
|
|
|
removedChainBlockHashes := make([]string, len(selectedParentChainChanges.Removed))
|
|
for i, removed := range selectedParentChainChanges.Removed {
|
|
removedChainBlockHashes[i] = removed.String()
|
|
}
|
|
|
|
addedChainBlocks := make([]string, len(selectedParentChainChanges.Added))
|
|
for i, added := range selectedParentChainChanges.Added {
|
|
addedChainBlocks[i] = added.String()
|
|
}
|
|
|
|
var acceptedTransactionIDs []*appmessage.AcceptedTransactionIDs
|
|
if includeAcceptedTransactionIDs {
|
|
var err error
|
|
acceptedTransactionIDs, err = ctx.getAndConvertAcceptedTransactionIDs(selectedParentChainChanges)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return appmessage.NewVirtualSelectedParentChainChangedNotificationMessage(
|
|
removedChainBlockHashes, addedChainBlocks, acceptedTransactionIDs), nil
|
|
}
|
|
|
|
func (ctx *Context) getAndConvertAcceptedTransactionIDs(selectedParentChainChanges *externalapi.SelectedChainPath) (
|
|
[]*appmessage.AcceptedTransactionIDs, error) {
|
|
|
|
acceptedTransactionIDs := make([]*appmessage.AcceptedTransactionIDs, len(selectedParentChainChanges.Added))
|
|
|
|
for i, addedChainBlock := range selectedParentChainChanges.Added {
|
|
blockAcceptanceData, err := ctx.Domain.Consensus().GetBlockAcceptanceData(addedChainBlock)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
acceptedTransactionIDs[i] = &appmessage.AcceptedTransactionIDs{
|
|
AcceptingBlockHash: addedChainBlock.String(),
|
|
AcceptedTransactionIDs: nil,
|
|
}
|
|
for _, blockAcceptanceData := range blockAcceptanceData {
|
|
for _, transactionAcceptanceData := range blockAcceptanceData.TransactionAcceptanceData {
|
|
if transactionAcceptanceData.IsAccepted {
|
|
acceptedTransactionIDs[i].AcceptedTransactionIDs =
|
|
append(acceptedTransactionIDs[i].AcceptedTransactionIDs,
|
|
consensushashing.TransactionID(transactionAcceptanceData.Transaction).String())
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return acceptedTransactionIDs, nil
|
|
}
|