-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add "GetMempoolEntriesByAddresses" to kaspad RPC. (#2022)
* add "GetMempoolEntriesByAddresses" to kaspad RPC. * fmt formatting. * some things I forgot * update rpc.md * forgot to add handler * fix fmt * bug fix, implicat testing & error handling * address reveiw * address reveiw
- Loading branch information
Showing
17 changed files
with
920 additions
and
212 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package appmessage | ||
|
||
// MempoolEntryByAddress represents MempoolEntries associated with some address | ||
type MempoolEntryByAddress struct { | ||
Address string | ||
Receiving []*MempoolEntry | ||
Sending []*MempoolEntry | ||
} | ||
|
||
// GetMempoolEntriesByAddressesRequestMessage is an appmessage corresponding to | ||
// its respective RPC message | ||
type GetMempoolEntriesByAddressesRequestMessage struct { | ||
baseMessage | ||
Addresses []string | ||
} | ||
|
||
// Command returns the protocol command string for the message | ||
func (msg *GetMempoolEntriesByAddressesRequestMessage) Command() MessageCommand { | ||
return CmdGetMempoolEntriesByAddressesRequestMessage | ||
} | ||
|
||
// NewGetMempoolEntriesByAddressesRequestMessage returns a instance of the message | ||
func NewGetMempoolEntriesByAddressesRequestMessage(addresses []string) *GetMempoolEntriesByAddressesRequestMessage { | ||
return &GetMempoolEntriesByAddressesRequestMessage{ | ||
Addresses: addresses, | ||
} | ||
} | ||
|
||
// GetMempoolEntriesByAddressesResponseMessage is an appmessage corresponding to | ||
// its respective RPC message | ||
type GetMempoolEntriesByAddressesResponseMessage struct { | ||
baseMessage | ||
Entries []*MempoolEntryByAddress | ||
|
||
Error *RPCError | ||
} | ||
|
||
// Command returns the protocol command string for the message | ||
func (msg *GetMempoolEntriesByAddressesResponseMessage) Command() MessageCommand { | ||
return CmdGetMempoolEntriesByAddressesResponseMessage | ||
} | ||
|
||
// NewGetMempoolEntriesByAddressesResponseMessage returns a instance of the message | ||
func NewGetMempoolEntriesByAddressesResponseMessage(entries []*MempoolEntryByAddress) *GetMempoolEntriesByAddressesResponseMessage { | ||
return &GetMempoolEntriesByAddressesResponseMessage{ | ||
Entries: entries, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package rpchandlers | ||
|
||
import ( | ||
"github.com/kaspanet/kaspad/app/appmessage" | ||
"github.com/kaspanet/kaspad/app/rpc/rpccontext" | ||
"github.com/kaspanet/kaspad/domain/consensus/utils/txscript" | ||
"github.com/kaspanet/kaspad/infrastructure/network/netadapter/router" | ||
"github.com/kaspanet/kaspad/util" | ||
) | ||
|
||
// HandleGetMempoolEntriesByAddresses handles the respectively named RPC command | ||
func HandleGetMempoolEntriesByAddresses(context *rpccontext.Context, _ *router.Router, request appmessage.Message) (appmessage.Message, error) { | ||
|
||
transactions := context.Domain.MiningManager().AllTransactions() | ||
getMempoolEntriesByAddressesRequest := request.(*appmessage.GetMempoolEntriesByAddressesRequestMessage) | ||
mempoolEntriesByAddresses := make([]*appmessage.MempoolEntryByAddress, 0) | ||
|
||
for _, addressString := range getMempoolEntriesByAddressesRequest.Addresses { | ||
|
||
_, err := util.DecodeAddress(addressString, context.Config.ActiveNetParams.Prefix) | ||
if err != nil { | ||
errorMessage := &appmessage.GetUTXOsByAddressesResponseMessage{} | ||
errorMessage.Error = appmessage.RPCErrorf("Could not decode address '%s': %s", addressString, err) | ||
return errorMessage, nil | ||
} | ||
|
||
sending := make([]*appmessage.MempoolEntry, 0) | ||
receiving := make([]*appmessage.MempoolEntry, 0) | ||
|
||
for _, transaction := range transactions { | ||
|
||
for _, input := range transaction.Inputs { | ||
_, transactionSendingAddress, err := txscript.ExtractScriptPubKeyAddress( | ||
input.UTXOEntry.ScriptPublicKey(), | ||
context.Config.ActiveNetParams) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if addressString == transactionSendingAddress.String() { | ||
rpcTransaction := appmessage.DomainTransactionToRPCTransaction(transaction) | ||
sending = append( | ||
sending, | ||
&appmessage.MempoolEntry{ | ||
Fee: transaction.Fee, | ||
Transaction: rpcTransaction, | ||
}, | ||
) | ||
break //one input is enough | ||
} | ||
} | ||
|
||
for _, output := range transaction.Outputs { | ||
_, transactionReceivingAddress, err := txscript.ExtractScriptPubKeyAddress( | ||
output.ScriptPublicKey, | ||
context.Config.ActiveNetParams, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if addressString == transactionReceivingAddress.String() { | ||
rpcTransaction := appmessage.DomainTransactionToRPCTransaction(transaction) | ||
receiving = append( | ||
receiving, | ||
&appmessage.MempoolEntry{ | ||
Fee: transaction.Fee, | ||
Transaction: rpcTransaction, | ||
}, | ||
) | ||
break //one output is enough | ||
} | ||
} | ||
|
||
//Only append mempoolEntriesByAddress, if at least 1 mempoolEntry for the address is found. | ||
//This mimics the behaviour of GetUtxosByAddresses RPC call. | ||
if len(sending) > 0 || len(receiving) > 0 { | ||
mempoolEntriesByAddresses = append( | ||
mempoolEntriesByAddresses, | ||
&appmessage.MempoolEntryByAddress{ | ||
Address: addressString, | ||
Sending: sending, | ||
Receiving: receiving, | ||
}, | ||
) | ||
} | ||
|
||
} | ||
} | ||
|
||
return appmessage.NewGetMempoolEntriesByAddressesResponseMessage(mempoolEntriesByAddresses), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.