mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-09-13 04:50:11 +00:00
40 lines
1.7 KiB
Go
40 lines
1.7 KiB
Go
package rpchandlers
|
|
|
|
import (
|
|
"github.com/kaspanet/kaspad/app/appmessage"
|
|
"github.com/kaspanet/kaspad/app/rpc/rpccontext"
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/consensushashing"
|
|
"github.com/kaspanet/kaspad/domain/miningmanager/mempool"
|
|
"github.com/kaspanet/kaspad/infrastructure/network/netadapter/router"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// HandleSubmitTransaction handles the respectively named RPC command
|
|
func HandleSubmitTransaction(context *rpccontext.Context, _ *router.Router, request appmessage.Message) (appmessage.Message, error) {
|
|
submitTransactionRequest := request.(*appmessage.SubmitTransactionRequestMessage)
|
|
|
|
domainTransaction, err := appmessage.RPCTransactionToDomainTransaction(submitTransactionRequest.Transaction)
|
|
if err != nil {
|
|
errorMessage := &appmessage.SubmitTransactionResponseMessage{}
|
|
errorMessage.Error = appmessage.RPCErrorf("Could not parse transaction: %s", err)
|
|
return errorMessage, nil
|
|
}
|
|
|
|
transactionID := consensushashing.TransactionID(domainTransaction)
|
|
err = context.ProtocolManager.AddTransaction(domainTransaction, submitTransactionRequest.AllowOrphan)
|
|
if err != nil {
|
|
if !errors.As(err, &mempool.RuleError{}) {
|
|
return nil, err
|
|
}
|
|
|
|
log.Debugf("Rejected transaction %s: %s", transactionID, err)
|
|
// Return the ID also in the case of error, so that clients can match the response to the correct transaction submit request
|
|
errorMessage := appmessage.NewSubmitTransactionResponseMessage(transactionID.String())
|
|
errorMessage.Error = appmessage.RPCErrorf("Rejected transaction %s: %s", transactionID, err)
|
|
return errorMessage, nil
|
|
}
|
|
|
|
response := appmessage.NewSubmitTransactionResponseMessage(transactionID.String())
|
|
return response, nil
|
|
}
|