mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-06 14:16:43 +00:00

* [NOD-381] Publish transaction messages to MQTT * [NOD-381] Remove redundant variable * [NOD-381] Send payload as string * [NOD-381] Add Error handling * [NOD-381] Respond with TransactionResponse * [NOD-381] Use transactionResponse for notifications * [NOD-381] Move code to appropriate places * [NOD-381] Pass raw block instead of txId * [NOD-381] Add comments to public functions * [NOD-381] Remove print statement * [NOD-381] Pass transaction instead of block; Use pointers so default will be nil; * [NOD-381] Use pointers so value could be nil * [NOD-381] Change variable name * [NOD-381] Set QoS to 2 * [NOD-381] Move isConnected to MQTT, so client won't have to worry about it; General code refactors;
66 lines
2.4 KiB
Go
66 lines
2.4 KiB
Go
package controllers
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"github.com/daglabs/btcd/apiserver/apimodels"
|
|
"github.com/daglabs/btcd/apiserver/dbmodels"
|
|
"github.com/daglabs/btcd/btcjson"
|
|
)
|
|
|
|
func convertTxDBModelToTxResponse(tx *dbmodels.Transaction) *apimodels.TransactionResponse {
|
|
txRes := &apimodels.TransactionResponse{
|
|
TransactionHash: tx.TransactionHash,
|
|
TransactionID: tx.TransactionID,
|
|
SubnetworkID: tx.Subnetwork.SubnetworkID,
|
|
LockTime: tx.LockTime,
|
|
Gas: tx.Gas,
|
|
PayloadHash: tx.PayloadHash,
|
|
Payload: hex.EncodeToString(tx.Payload),
|
|
Inputs: make([]*apimodels.TransactionInputResponse, len(tx.TransactionInputs)),
|
|
Outputs: make([]*apimodels.TransactionOutputResponse, len(tx.TransactionOutputs)),
|
|
Mass: tx.Mass,
|
|
}
|
|
if tx.AcceptingBlock != nil {
|
|
txRes.AcceptingBlockHash = &tx.AcceptingBlock.BlockHash
|
|
txRes.AcceptingBlockBlueScore = &tx.AcceptingBlock.BlueScore
|
|
}
|
|
for i, txOut := range tx.TransactionOutputs {
|
|
txRes.Outputs[i] = &apimodels.TransactionOutputResponse{
|
|
Value: txOut.Value,
|
|
ScriptPubKey: hex.EncodeToString(txOut.ScriptPubKey),
|
|
Address: txOut.Address.Address,
|
|
Index: txOut.Index,
|
|
}
|
|
}
|
|
for i, txIn := range tx.TransactionInputs {
|
|
txRes.Inputs[i] = &apimodels.TransactionInputResponse{
|
|
PreviousTransactionID: txIn.PreviousTransactionOutput.Transaction.TransactionID,
|
|
PreviousTransactionOutputIndex: txIn.PreviousTransactionOutput.Index,
|
|
SignatureScript: hex.EncodeToString(txIn.SignatureScript),
|
|
Sequence: txIn.Sequence,
|
|
Address: txIn.PreviousTransactionOutput.Address.Address,
|
|
}
|
|
}
|
|
return txRes
|
|
}
|
|
|
|
func convertBlockModelToBlockResponse(block *dbmodels.Block) *apimodels.BlockResponse {
|
|
blockRes := &apimodels.BlockResponse{
|
|
BlockHash: block.BlockHash,
|
|
Version: block.Version,
|
|
HashMerkleRoot: block.HashMerkleRoot,
|
|
AcceptedIDMerkleRoot: block.AcceptedIDMerkleRoot,
|
|
UTXOCommitment: block.UTXOCommitment,
|
|
Timestamp: uint64(block.Timestamp.Unix()),
|
|
Bits: block.Bits,
|
|
Nonce: block.Nonce,
|
|
BlueScore: block.BlueScore,
|
|
IsChainBlock: block.IsChainBlock,
|
|
Mass: block.Mass,
|
|
}
|
|
if block.AcceptingBlock != nil {
|
|
blockRes.AcceptingBlockHash = btcjson.String(block.AcceptingBlock.BlockHash)
|
|
}
|
|
return blockRes
|
|
}
|