-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmempool_handler.go
114 lines (95 loc) · 3.68 KB
/
mempool_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"encoding/json"
"fmt"
"net/http"
"github.com/NickP005/go_mcminterface"
)
var TXCLEANFILE_PATH = "mochimo/bin/d/txclean.dat"
// MempoolTransactionRequest is utilized to retrieve a transaction from the mempool.
type MempoolTransactionRequest struct {
NetworkIdentifier NetworkIdentifier `json:"network_identifier"`
TransactionIdentifier TransactionIdentifier `json:"transaction_identifier"`
}
// MempoolTransactionResponse contains an estimate of a mempool transaction.
type MempoolTransactionResponse struct {
Transaction Transaction `json:"transaction"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}
// MempoolResponse contains all transaction identifiers in the mempool for a particular network identifier.
type MempoolResponse struct {
TransactionIdentifiers []TransactionIdentifier `json:"transaction_identifiers"`
}
// mempoolHandler handles requests to fetch all transaction identifiers in the mempool.
func mempoolHandler(w http.ResponseWriter, r *http.Request) {
// Check for the correct network identifier
if _, err := checkIdentifier(r); err != nil {
mlog(3, "§bmempoolHandler(): §4Wrong network identifier")
giveError(w, ErrWrongNetwork) // Wrong network identifier
return
}
// Fetch transactions from the mempool
mempool, err := getMempool(TXCLEANFILE_PATH) // Replace with actual mempool path
if err != nil {
mlog(3, "§bmempoolHandler(): §4Error reading mempool: §c%s", err)
giveError(w, ErrInternalError) // Internal error
return
}
// Create a list of transaction identifiers
var txIdentifiers []TransactionIdentifier
for _, tx := range mempool {
txIdentifiers = append(txIdentifiers, TransactionIdentifier{
Hash: fmt.Sprintf("0x%x", tx.Tlr.ID[:]),
})
}
// Create the response
response := MempoolResponse{
TransactionIdentifiers: txIdentifiers,
}
// Set headers and encode the response as JSON
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
// mempoolTransactionHandler handles requests to fetch a specific transaction from the mempool.
func mempoolTransactionHandler(w http.ResponseWriter, r *http.Request) {
var req MempoolTransactionRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
mlog(3, "§bmempoolTransactionHandler(): §4Error decoding request: §c%s", err)
giveError(w, ErrInvalidRequest) // Invalid request
return
}
if req.NetworkIdentifier.Blockchain != "mochimo" || req.NetworkIdentifier.Network != "mainnet" {
mlog(3, "§bmempoolTransactionHandler(): §4Wrong network identifier")
giveError(w, ErrWrongNetwork) // Wrong network identifier
return
}
// Fetch transactions from the mempool
mempool, err := getMempool(TXCLEANFILE_PATH) // Replace with actual mempool path
if err != nil {
mlog(3, "§bmempoolTransactionHandler(): §4Error reading mempool: §c%s", err)
giveError(w, ErrInternalError) // Internal error
return
}
// Search for the transaction in the mempool
var foundTx *go_mcminterface.TXENTRY
for _, tx := range mempool {
if fmt.Sprintf("0x%x", tx.Tlr.ID[:]) == req.TransactionIdentifier.Hash {
foundTx = &tx
break
}
}
if foundTx == nil {
mlog(3, "§bmempoolTransactionHandler(): §4Transaction not found")
giveError(w, ErrTXNotFound) // Transaction not found error
return
}
// Convert the found transaction to the response format
transaction := getTransactionsFromBlockBody([]go_mcminterface.TXENTRY{*foundTx}, go_mcminterface.WotsAddress{}, false)[0]
// Create the response
response := MempoolTransactionResponse{
Transaction: transaction,
}
// Set headers and encode the response as JSON
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}